I have to create a regex to match exact string – /op
, /kl
, /xz
Individual Regex works:
- new RegExp(‘/op’).test("/op")
- new RegExp(‘/kl’).test("/kl")
- new RegExp(‘/xz’).test("/xz")
How to merge this check into 1 Regex ?
I tried new RegExp('(/op) | (/xz)').test("/op")
but it gives false
2
Answers
new RegExp('^\/(op|kl|xz)$')
did the trick to match exact pattern.According to regex101.com, this works:
With the long version being: