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?

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.