4

I checked the sizzle code and see a definition.

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

I want to know how to find out what string(s) this regular expression will match?

4
  • 4
    The first time I saw a reverse question on regex.
    – nhahtdh
    Jul 24, 2012 at 13:46
  • Why the downvote and the close votes? If this piece of work was actually standing there uncommented, then this is a valid question
    – kostja
    Jul 24, 2012 at 13:53
  • I don't even believe this regex is valid (looks like it's a parenthesis-pairing off) Jul 24, 2012 at 13:54
  • @BradChristie: It is valid in JS.
    – nhahtdh
    Jul 24, 2012 at 13:54

3 Answers 3

4

See this article. Explanation in a multiline regex:

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 matching groups: A "validated" selector expression, eventual comma, and everything after that. It will continuosly be called on the selector to split it up in parts, see the Sizzle constructor for details.

1

It would be speculative. However, using RegexBuddy you can 'document' and visualize the expression.

Documentation using RegexBuddy

Because it is a paid application i've exported the comments on pastebin. Hope this will help you. (Note that it does not support Sizzle and that i've used JavaScript language to document the expression).

0

To learn how to do it by hand, you can simply break it down like this (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 regex is generally written, so in this case it would be:

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

So let's strip away the modifier and the start and end and evaluate just the regex:

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

Still gobbledygook, but let's continue: The regex is for capturing content and we know that unescaped (meaning they use a backslash before the character being escaped \) parenthesis are capturing braces, let's add some newlines around each parenthesis (only for the purposes of examination!!!) and pipes mean "or"

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

Square braces mean "selectively match what is inside of this as any value from a choice-set" and so now I'm going to add a few comments, and let you work out the rest (also, it does look like some parens 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
    )*
  )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.