2

我有以下代码:

    <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>

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

4

1 回答 1

3

我终于用以下代码解决了这个问题:

    <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>
于 2013-04-23T15:28:17.610 回答