0

This regular expressioncomes from JQuery, and the | means or, but which two parts are included to choose?

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

Is it [^>]*$ | #([\w\-]*)$ or (<[\w\W]+>)[^>]*$ | #([\w\-]*)$) or something else?

Micha Wiedenmann
  • 17,330
  • 20
  • 79
  • 123
YuC
  • 1,503
  • 3
  • 16
  • 22
  • 1
    Try looking at it on [regexper.com](http://www.regexper.com/#%5E(%3F%3A%5B%5E%23%3C%5D*(%3C%5B%5Cw%5CW%5D%2B%3E)%5B%5E%3E%5D*%24%7C%23(%5B%5Cw%5C-%5D*)%24)). – Chris Martin Mar 05 '14 at 03:29

4 Answers4

3

It's [^#<]*(<[\w\W]+>)[^>]*$ and #([\w\-]*)$

Regular expression visualization

Debuggex Demo

Andrew
  • 5,128
  • 1
  • 14
  • 21
3

Simply: x|y

Matches either x or y.

For example: /green|red/ matches green in green apple and red in red apple.

See regular expression on mdn

HamZa
  • 13,530
  • 11
  • 51
  • 70
Bhojendra Rauniyar
  • 73,156
  • 29
  • 131
  • 187
  • This answer has been added to the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), under "Other". – aliteralmind Apr 10 '14 at 01:04
2

Writing it multiline with indentation helps:

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

Your second guess is correct. The | has lower precedence than the concatenation (i.e. writing expressions after each other).

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
0

It's [^#<]*(<[\w\W]+>)[^>]*$ or #([\w\-]*)$, and both in a non-capturing group.

/^(?:EXP)$)/ # EXP => A|B

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

/^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/
xdazz
  • 149,740
  • 33
  • 229
  • 258