skip to Main Content

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


  1. Chosen as BEST ANSWER

    new RegExp('^\/(op|kl|xz)$') did the trick to match exact pattern.


  2. According to regex101.com, this works:

    //(op|kl|xy)/g
    

    With the long version being:

    new RegExp('\/(op|kl|xy)', 'g')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search