Gnijuohz
Gnijuohz

Reputation: 3364

Can somebody explain this RegEx to me?

I see this line of code and the regular expression just panics me...

quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/

Can someone please explain little by little what it does? Thanks,G

Upvotes: 0

Views: 127

Answers (2)

Blender
Blender

Reputation: 298552

Here's what I can extract:

  • ^ beginning of string.
  • (?: non-matching group.
  • [^#<]* any number of consecutive characters that aren't # or <.
  • (<[\w\W]+>) a group that matches strings like <anything_goes_here>.
  • [^>]* any number of characters in sequence that aren't a >.

The part after the | denotes a second regex to try if the first one fails. That one is #([\w\-]*):

  • # matches the # character. Not that complex.
  • ([\w\-]*) is a group that matches any number of word characters or dashes. Basically Things-of-this-form
  • $ marks the end of the regex.

I'm no regex pro, so please correct me if I am wrong.

Upvotes: 2

QuantumBlack
QuantumBlack

Reputation: 1546

^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)

Assert position at the start of the string «^»
Match the regular expression below «(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)»
   Match either the regular expression below (attempting the next alternative only if this one fails) «[^#<]*(<[\w\W]+>)[^>]*$»
      Match a single character NOT present in the list "#<" «[^#<]*»
         Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
      Match the regular expression below and capture its match into backreference number 1 «(<[\w\W]+>)»
         Match the character "<" literally «<»
         Match a single character present in the list below «[\w\W]+»
            Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
            Match a single character that is a "word character" (letters, digits, etc.) «\w»
            Match a single character that is a "non-word character" «\W»
         Match the character ">" literally «>»
      Match any character that is not a ">" «[^>]*»
         Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
      Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
   Or match regular expression number 2 below (the entire group fails if this one fails to match) «#([\w\-]*)$»
      Match the character "#" literally «#»
      Match the regular expression below and capture its match into backreference number 2 «([\w\-]*)»
         Match a single character present in the list below «[\w\-]*»
            Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
            Match a single character that is a "word character" (letters, digits, etc.) «\w»
            A - character «\-»
      Assert position at the end of the string (or before the line break at the end of the string, if any) «$»


Created with RegexBuddy

Upvotes: 2

Related Questions