使用jstl搜索并替换单引号和反斜杠

时间:2013-04-23 09:57:38

标签: jsp jstl

我有以下代码:

    <c:set var="myContent" value="${row.content}"/>
    <c:set var="search" value="'" />
    <c:set var="replace" value="\\'" />
    <c:set var="myContent" value="${fn:replace(myContent, search, replace)}"/>

     <script language="JavaScript" type="text/JavaScript">
            window.addEventListener("load", function(){
            element.innerHTML='${myContent}';
            }, true);
      </script>

它只替换单引号字符,如果我还想替换反斜杠()怎么办? 任何的想法?提前致谢

1 个答案:

答案 0 :(得分:2)

我最终使用以下代码管理问题:

    <c:set var="myContent" value="${row.content}"/>
    <c:set var="singlequote" value="'"/>
    <c:set var="backslash" value="\\"/>
    <c:if test="${fn:contains(myContent, singlequote) && !fn:contains(myContent,backslash)}">
            <c:set var="search" value="'" />
            <c:set var="replace" value="\\'" />
            <c:set var="myContent" value="${fn:replace(myContent, search, replace)}"/>
    </c:if>
    <c:if test="${fn:contains(myContent, backslash) && !fn:contains(myContent,singlequote)}">
            <c:set var="search" value="\\" />
            <c:set var="replace" value="\\\\" />
            <c:set var="myContent" value="${fn:replace(myContent, search, replace)}"/>
    </c:if>
    <c:if test="${fn:contains(myContent, singlequote) && fn:contains(myContent,backslash)}">
            <c:set var="search" value="\\"/>
            <c:set var="replace" value="\\\\" />
            <c:set var="myContent" value="${fn:replace(myContent, search, replace)}"/>
            <c:set var="find" value="'"/>
            <c:set var="change" value="\\'" />
            <c:set var="myContent" value="${fn:replace(myContent, find, change)}"/>
    </c:if>


    <script language="JavaScript" type="text/JavaScript">
          window.addEventListener("load", function(){
            element.innerHTML='${myContent}';
            }, true);
     </script>