有没有办法用正则表达式在javascript中获取一段不在引号(单引号或双引号)之间的代码?
如果我有这个字符串:
'this is a test "this shouldn't be taken"'
结果应该是:
'this is a test'
有没有办法用正则表达式在javascript中获取一段不在引号(单引号或双引号)之间的代码?
如果我有这个字符串:
'this is a test "this shouldn't be taken"'
结果应该是:
'this is a test'
这应该删除单引号或双引号之间的任何内容,它适用于多行字符串(包含 \n 或 \r 的字符串),它还应该处理转义引号:
var removeQuotes = /(['"])(?:\\?[\s\S])*?\1/g;
var test = 'this is a test "this shouldn\'t be taken"';
test.replace(removeQuotes, ""); // 'this is a test '
test = 'this is a test "this sho\\"uldn\'t be taken"';
test.replace(removeQuotes, ""); // 'this is a test '
myString.replace(/".*?"/g, '')
将从 myString 中删除双引号之间的任何字符串。但是,它不处理转义的双引号。
您可以使用 javascriptreplace函数删除字符串的引号部分:
str = 'this is a test "this shouldn\'t be taken"';
str_without_quotes = str.replace(/(['"]).*?\1/g, "") // => 'this is a test '