What does it mean? which string will match the expression?

I checked the sizzle code and looked at the definition.

var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g, 

I want to know how to find out which string (s) matches this regular expression?

+4
source share
3 answers

See this article . Explanation in multiline regular expression:

 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 

This expression contains three comparable groups: a "checked" selector expression, a trailing comma, and all after that. It will be constantly called a selector to divide it in parts, see Sizzle Designer for more details.

+4
source

That would be speculative. However, using RegexBuddy , you can โ€œdocumentโ€ and visualize the expression.

Documentation using RegexBuddy

Since this is a paid application, I exported comments on pastebin . Hope this helps you. (Note that it does not support Sizzle and that I used JavaScript to document the expression).

+1
source

To find out how to do this manually, you can simply break it this way (this is a task that will surely help you in the future):

 var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g, 

The first part is to know how a regular expression is usually written, so in this case it will be:

 var variableName = /stuff/g; ^ ^ ^^ delimiters/---+--/| | | regex / \global modifier, can also have a case modifier 

So, drop the modifier and the beginning and the end and evaluate only the regular expression:

 ((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*) 

Still gobbledygook, but go ahead: the regex is for capturing content, and we know that unescaped (which means they use backslashes before the character that was escaped \ ) the brackets capture the curly braces, add some new lines around each parenthesis (for research purposes only !!!), and pipes mean "or"

 ( (?: \( (?: \( [^()]+ \) | [^()]+ )+ \) | \[ (?: \[[^\[\]]*\] | ['"][^'"]*['"] | [^\[\]'"]+ )+ \] | \\. | [^ >+~, ( \[\\]+)+ | [>+~] ) (\s*,\s*)? ( (?: . | \r | \n )* ) 

The braces mean โ€œselectively match what's inside it like any value from a selection set,โ€ and so now I'm going to add a few comments and give you the opportunity to work out the rest (also, it looks like some parsers are missing)

 ( (?: //<-- 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 )* ) 
0
source

All Articles