jQuery:如何使用反斜杠转义单引号和双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18491606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: How to escape single and double quotes with backslash
提问by no1uknow
I want to escape single and double quotes with a backslash in one line rather than two.
我想在一行而不是两行中使用反斜杠来转义单引号和双引号。
Example for single quote:
单引号示例:
str = str.replace(/'/g, "\'");
Is there a way to do this at the same time for double quotes included?
对于包含的双引号,有没有办法同时做到这一点?
Sniffer Answered this very well below, and I ended up escaping all characters we needed as follows:
Sniffer 在下面很好地回答了这个问题,我最终转义了我们需要的所有字符,如下所示:
str = str.replace(/(['"&:;])/g, "\");
Thanks again Sniffer for the quick response!
再次感谢嗅探器的快速响应!
回答by Ibrahim Najjar
Try this:
尝试这个:
str = str.replace(/(['"])/g, "\");