在字符串 " 符号前面添加 \ 转义符号 除了img标签里的 " ,也就是<img src="456.jpg" />里的 "不处理其它都处理。
html页面的字符串如:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<style type="text/css"></style>. 以下省略省略.....
<img src="123.jpg" />
<div>
<img src="456.jpg" />
</div>
</html>
转义完为:
<!DOCTYPE HTML>
<html>
<head>
<meta charset=\"UTF-8\" />
<style type=\"text/css\"></style>. 以下省略省略.....
<img src="123.jpg" />
<div>
<img src="456.jpg" />
</div>
</html>
谢谢各位!
------解决思路----------------------
String s1="<meta charset=\"UTF-8\" /><style type=\"text/css\"></style>. 以下省略省略.....<img src=\"123.jpg\" /><div><img src=\"456.jpg\" /></div>";
System.out.println(s1.replaceAll("\"", "\\\\\"").replaceAll("(<img\\s+src=)\\\\\"(\\d+\\.jpg)\\\\\"(\\s+/>)", "$1\"$2\"$3"));