转译成""才可以alert出来!一、我现在发现
1换行
2单引
这两个符号需要转!否则就alert不出来,但是不知道其他还有什么需要转啊?二、还有一个问题是我发现只有转换成空才行
_body = _body.replaceAll("\\r?\\n","");
_body = _body.replaceAll("'","");
如果我转换成
_body = _body.replaceAll("\\r?\\n","\\n");
_body = _body.replaceAll("'","\\'");
因为我想保留语意所以我想用javascript的转义符来替换但是都失败了!不知道该怎么做才能
保留原来的语意而不是替换成空呢?<%
String _body = "'Test1\r\nTest2\r\nTest3\r\nTest4\r\nTest5'";
out.println("object_s:"+_body+"<br>");_body = _body.replaceAll("\\r?\\n","");
_body = _body.replaceAll("'","");
out.println("object_e:"+_body+"<br>");
out.println(_body);
%>
<script>
alert('<%=_body%>');
</script>

解决方案 »

  1.   


    <%
    String _body = "'Test1\r\nTest2\r\nTest3\r\nTest4\r\nTest5'";
    out.println("object_s:" + _body + "<br>"); _body = _body.replaceAll("\\r\\n", "<br>");
    //_body = _body.replaceAll("'","");
    out.println("object_e:" + _body + "<br>");
    out.println(_body);
    %><script>function replaceall(str, str1, str2)
    {
    while(str.indexOf(str1) != -1){
    str = str.replace(str1, str2);
    }
    return str;
    }var str = "<%=_body%>";alert(replaceall(str, "<br>", "\r\n"));</script>
    单引号的问题,你只要在js定义的时候外面加双引号就好了,如果内容有双引号,最好用\"替代,
    至于回车问题解决方案不只这一种,推荐一个而已。
      

  2.   

    _body = _body.replaceAll("\\r\\n", "<br>");
      

  3.   

    _body = _body.replaceAll("'",""); 单引呢?替换成什么???????????????????