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 is supporting Monica
  • 142,542
  • 55
  • 282
  • 355
George Hernando
  • 2,480
  • 7
  • 36
  • 56

1 Answers1

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

In an easier way to see:

/
    \(
        [
            ^[\]<>
        ]*
\)
|
\[
    [
        ^()<>
    ]
\]*
|
\<
    [
        ^[\]()
    ]*
\>
/
Danilo Valente
  • 11,037
  • 8
  • 52
  • 66