1

Below is my code

/config\/info\/newplan/.test(string)

which will return true when find /config/info/newplan/ in string.

However, I would like to test different condition in the same time like below

/config\/info\/newplan/.test(string) || /config\/info\/oldplan/.test(string) || /config\/info\/specplan/.test(string)

which will return true if the string end up with either "newplan" or "oldplan" or "specplan"

My question is how to make a better code and not write "/config/\info/\xxxx\ so many times?

Dreams
  • 6,942
  • 7
  • 37
  • 57

2 Answers2

1

Use an alternation group:

/config\/info\/(?:new|old|spec)plan/.test(string)
                ^^^^^^^^^^^^^^^ 

See the regex demo.

Pattern details:

  • config\/info\/ - a literal config/info/ substring
  • (?:new|old|spec) - a non-capturing group (where | separates alternatives) matching any one of the substrings: new, old or spec
  • plan - a literal plan substring
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
1

this would be your bet

config\/info\/(newplan|oldplan|specplan)\/
OR
config\/info\/(newplan|oldplan|specplan)\/.test(string)

please see the example at [https://regex101.com/r/NyP1HP/1] as it doesn't allow other possibilities like following

/config/info/new1plan/
/config/info/newoldplan/
/config/info/specplan1/
praxnet
  • 3,976
  • 2
  • 18
  • 37