我试图逃脱引号(和apostrofes和逃逸字符)在JavaScript中的文本字符串:逃离报价
var text = 'Escape " and \' and /.';
var rx = new RegExp('/([\'"])/g');
console.log(text, ' ==> ', text.replace(rx,'//\1'));
我期望能够输出为Escape /" and /' and //.
,而是我得到Escape " and ' and /.
。
我似乎无法得到这个工作,不知道什么是错的。
这里有一个的jsfiddle:http://jsfiddle.net/hvtgf/
你为什么想到'/'?转义字符是'\\'.. –
此外,请停止使用新的RegExp('/([\''])/ g'); Javascript正则表达式是文字,就像每次你想要做新字符串定义一个字符串 –
@Rob W,因为对于我保存的格式,我需要'/',并且明确地在替换字符串中设置'/' – Martijn