我有以下字符串:
here "is 'a' \"string\" that" does contain a 'lot of "weird"' stuff "i" to 'find'
我想提取is 'a' \"string\" that,lot of "weird"和.ifind
有任何想法吗?谢谢!
我有以下字符串:
here "is 'a' \"string\" that" does contain a 'lot of "weird"' stuff "i" to 'find'
我想提取is 'a' \"string\" that,lot of "weird"和.ifind
有任何想法吗?谢谢!
你可以试试这个模式:
/"((?:[^"\\]+|\\"|\\)*)"|'((?:[^'\\]+|\\'|\\)*)'/g
双引号内的内容在第 1 组,单引号内的内容在第 2 组。
注意:这个解决方案对于转义引号不是完全防水的,因为如果\"可以检测到上述模式的 ,前面有另一个\,那么 将\\被视为文字\并且引号不再转义!
为避免此陷阱,您可以检查是否有奇数个反斜杠替换\\"为(?:\\{2})*\\",那么第一个模式将如下所示:
/"((?:[^"\\]+|(?:\\{2})*\\"|\\)*)"|'((?:[^'\\]+|(?:\\{2})*\\'|\\)*)'/g
这种基于负后瞻的正则表达式应该适合您:
/(["']).*?(?<!\\)\1/g
不支持后向的正则表达式,例如 Javascript
/(['"])(?:\1|.*?[^\\]\1)/g