2

Is there an easy way to detect if a string has any of the three following combinations?:

...( ... ) ...
...[ ... ] ...
...< ... > ...

ie, does it contain a pair of matching parentheses, square or angle brackets? I can do it as 3 separate Regex statements. Can it be reduced to one?

gdoron
  • 147,333
  • 58
  • 291
  • 367
George Hernando
  • 2,550
  • 7
  • 41
  • 61

1 Answers1

4
/\([^[\]<>]*\)|\[[^()<>]\]*|\<[^[\]()]*\>/.test(str);

In an easier way to see:

/
    \(
        [
            ^[\]<>
        ]*
\)
|
\[
    [
        ^()<>
    ]
\]*
|
\<
    [
        ^[\]()
    ]*
\>
/
Danilo Valente
  • 11,270
  • 8
  • 53
  • 67