<a href="http://sighttp.qq.com/cgi-bin/"; target=_blank; onclick="var tempSrc='http://sighttp.qq.com/wpa.js?rantime='+Math.random()"></a>我想把上面的字符串中的"替换为\",怎么做啊?这样替换主要是为了在客户端拼接字符串,比如 "<span style='color:red'>您的qq状态</span>"+"<a href=\"http://sighttp.qq.com/cgi-bin/\"; target=_blank; onclick=\"var tempSrc='http://sighttp.qq.com/wpa.js?rantime='+Math.random()\"></a>"replaceAll("\"","\\\"")不起作用。谁有什么好方法交流下。
String str = "<a href=\"http://sighttp.qq.com/cgi-bin/\"; target=_blank; onclick=\"var tempSrc='http://sighttp.qq.com/wpa.js?rantime='+Math.random()\"> </a>";
System.out.println(str.replaceAll("\\\"", "\\\\\""));
}
html:<html>
<head>
<title></title>
<script>
function convert(){
div2.innerHTML = div1.innerHTML.replace(/\"/g,"\\\"");
}
</script>
</head><body onload="convert()">
替换前:
<div id="div1"><xmp><a href="http://sighttp.qq.com/cgi-bin/"; target=_blank; onclick="var tempSrc='http://sighttp.qq.com/wpa.js?rantime='+Math.random()"> </a></xmp></div>
替换后:
<div id="div2"></div>
</body>
</html>
我想你不需要那个Allreplace("\"","\\\"")我想这个就可以了。呵呵。无需正则替换,简单替换足以。