Search code examples
regexsizzle

what does this mean? what string will be matched using the expression?


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?


Solution

  • 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.