Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想在一行而不是两行中用反斜杠转义单引号和双引号。
单引号示例:
str = str.replace(/'/g, "\\'");
有没有办法在包含双引号的同时做到这一点?
嗅探器在下面很好地回答了这个问题,我最终转义了我们需要的所有字符,如下所示:
str = str.replace(/(['"&:;])/g, "\\$1");
再次感谢 Sniffer 的快速响应!
尝试这个:
str = str.replace(/(['"])/g, "\\$1");