我检查了嘶嘶声代码并查看了定义。
var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g,
我想知道如何找出这个正则表达式匹配的字符串?
见这篇文章。多行正则表达式中的解释:
var chunker = /
(
(?:
# One or more sets of parentheses that contain a string, or another set of
parentheses with a string
\(
(?:
\([^()]+\)
|
[^()]+
)+
\)
|
# Or one or more sets of brackets that contain a string, or another set of
brackets with a string
\[
(?:
\[[^\[\]]*\]
|
['"][^'"]*['"]
|
[^\[\]'"]+
)+
\]
|
# Or a backslash followed by any character
\\.
|
# Or one or more of any except these characters: > +~,([\
[^ >+~,(\[\\]+
)+
# or any one of these characters: >+~
|
[>+~]
)
# followed by zero or one commas, which may be surrounded by whitespace
(\s*,\s*)?
# followed by zero or more of anything, including line endings
((?:.|\r|\n)*)
/g
此表达式包含三个匹配组:“已验证”选择器表达式、最终逗号以及之后的所有内容。它将在选择器上不断被调用以将其拆分为多个部分,Sizzle
有关详细信息,请参阅构造函数。
这将是投机性的。但是,使用RegexBuddy ,您可以“记录”并可视化表达式。
因为它是一个付费应用程序,所以我在pastebin上导出了评论。希望这会帮助你。(请注意,它不支持 Sizzle,并且我使用 JavaScript 语言来记录表达式)。
要学习如何手工完成,您可以像这样简单地分解它(这是一项将来肯定会帮助您的任务):
var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g,
第一部分是了解正则表达式通常是如何编写的,所以在这种情况下它将是:
var variableName = /stuff/g;
^ ^ ^^
delimiters/---+--/|
| |
regex / \global modifier, can also have a case modifier
所以让我们去掉修饰符以及开始和结束,只计算正则表达式:
((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)
仍然是 gobbledygook,但让我们继续:正则表达式用于捕获内容,我们知道未转义(意味着他们在字符被转义之前使用反斜杠\
)括号捕获大括号,让我们在每个括号周围添加一些换行符(仅用于检查目的! !!)和管道的意思是“或”
(
(?:
\(
(?:
\(
[^()]+
\)
|
[^()]+
)+
\)
|
\[
(?:
\[[^\[\]]*\]
|
['"][^'"]*['"]
|
[^\[\]'"]+
)+
\]
|
\\.
|
[^ >+~,
(
\[\\]+)+
|
[>+~]
)
(\s*,\s*)?
(
(?:
.
|
\r
|
\n
)*
)
方括号的意思是“有选择地匹配其中的内容作为选择集中的任何值”,所以现在我要添加一些评论,让你解决剩下的问题(而且,看起来确实有些括号是失踪)
(
(?: //<-- start a non-capturing group (means, don't tell the app we matched)
\(
(?: //<-- start a non-capturing group (means, don't tell the app we matched)
\(
[^()]+ //one or more paren pairs inside a paren pair
\)
|
[^()]+ //or one or more paren pairs
)+
\)
|
\[
(?:
\[[^\[\]]*\]
|
['"][^'"]*['"]
|
[^\[\]'"]+
)+
\]
|
\\.
|
[^ >+~,
(
\[\\]+)+
|
[>+~] //either a tilde, plus or opening brace
)
(\s*,\s*)?
(
(?:
.
|
\r
|
\n
)*
)