Reputation: 8506
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?
Upvotes: 1
Views: 31
Reputation: 5790
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/
Upvotes: 1
Reputation: 627128
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
substringUpvotes: 1