我有几行输入
"callbackProxy({"7103/usatoday/high_impact/entertainment/main":{"_type_":"html","_expandable_":false,"_html_":"\x3chtml\x3e\x3chead\x3e\x3cstyle\x3e\x3c!--\na:link { color: #000000 }a:visited { color: #000000 }a:hover { color: #000000 }a:active { color: #000000 } --\x3e\x3c/style\x3e\x3cscript\x3e\x3c!--\n(function(){var d\x3d{};window.ss\x3dfunction(a){void 0!\x3d\x3dd[a]?d[a]++:d[a]\x3d1;var e\x3ddocument.getElementById(a),b\x3dd[a];if(window.css)css(a,\x22nm\x22,b,void 0,void 0)"
我需要将\x
替换为\\x
,将"
替换为\"
,然后再将其分配给字符串。因为只有\x
会给出错误,而字符串之间的"
也会出错。
提前致谢。
答案 0 :(得分:0)
如果您只是复制并粘贴此内容,则需要执行以下替换:
\x
至\u00
"
至\"
\u0022
至\"
(请注意,最后一个不是一个非常正常的要求,但是您的示例代码包含\x22
,它将被转换为\u0022
,这在Java字符串文字中是不允许的)
For the first you can replace matches of the regex \\x
with \u00
.
The second two can be accomplished with a single regex by replacing matches of "|\\u0022
with \"
.
答案 1 :(得分:0)
你可以这样做一行:
str = str.replaceAll("\"|(\\\\x)", "\\$0");
答案 2 :(得分:-1)
让我们假设字符串在变量x
中x = x.replace("\\x","\\\\x");
x = x.replace("\"","\\\"");
应该做的伎俩