[英]Javascript regex for semicolon not in complex nested quotes
Is there a way that a regex would match all of these instances where the semicolons would not match when in quotes but would match when not in quotes-有没有办法让正则表达式匹配所有这些实例,其中分号在引号中时不匹配,但在引号中时匹配 -
";" (match this instance); ";"
';' (match this instance); ';'
"";";" (match this instance); "";";"
';";"' (match this instance); ';";"'
This is just an example and the semicolons can be preceded by numbers letters etc. To provide a bit more context, take for example this case -这只是一个例子,分号前面可以有数字字母等。为了提供更多的上下文,以这种情况为例 -
'This is a \n ; test "statement;" ;' (match this instance); "This is also a test;'Statement;'"
The (match this instance);
(match this instance);
means that that instance of the semicolon should be matched.表示应该匹配该分号实例。 Also higher levels of "
and '
nesting is possible, so looking for a global solution.更高级别的"
和'
嵌套也是可能的,因此寻找全局解决方案。
Also it should match if the quotes are escaped with a backslash as so -如果引号用反斜杠转义,它也应该匹配 -
\"(match this instance);\"
"\";\""(match this instance);\"(match this instance);\"
You could match the character series that do not constitute a "valid" semi-colon, and then match the semi-colon, ...etc.您可以匹配不构成“有效”分号的字符系列,然后匹配分号,...等。 This way the whole text will be matched, but the semi-colons will be in separate matches:这样整个文本将被匹配,但分号将在单独的匹配中:
let regex = /(?:\\.|(["'])(?:\\.|.)*?\1|[^;])+|;/gms; let test = String.raw`";" (match this instance); ";" ';' (match this instance); ';' "";";" (match this instance); "";";" ';";"' (match this instance); ';";"' \"(match this instance);\" "\";\""(match this instance);\"(match this instance);\"` let matches = test.match(regex); console.log(matches);
Whenever a match is just one character (it will be a semi-colon) it is to be considered the actual match you'd be looking for.只要匹配只有一个字符(它将是一个分号),它就会被视为您要查找的实际匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.