我想替换字符串中的简单引号,而不替换转义的引号(在JavaScript中)。例如我想要这个字符串:
'Hello there! I\'m \'Doug\''
成为:
"Hello there! I\'m \'Doug\'"
注意:我进行了很多研究和测试(例如,在regex101.com上),发现某些内容(例如正则表达式)可以匹配不包含单词的行?但仍然不知道如何构建适当的正则表达式。
我尝试过的事情:
/[^\\]'/g
/\\{0}'/g
/[\\]{0}'/g
您可以将其String#replace
与callback
功能一起使用:
str = "'Hello there! I\\'m \\'Doug\\''";
str = str.replace(/(\\)?'/g, function($0, $1) {
return ($1) ? "\\'" : '"';
});
console.log( str );
//=> "Hello there! I\'m \'Doug\'"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句