将该
String#replaceAll()参数解释为正则表达式。该是转义字符都
String和
regex。你需要对正则表达式进行两次转义:
string.replaceAll("\\", "\\\\");但是你不必为此使用正则表达式,仅是因为你希望逐个字符地进行精确替换,并且这里不需要模式。因此
String#replace()就足够了:
string.replace("\", "\\");更新:根据注释,你似乎想在Javascript上下文中使用字符串。你最好使用它
StringEscapeUtils#escapeEcmascript()来覆盖更多字符。



