-1

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

Samuel
  • 1,128
  • 4
  • 14
  • 34

2 Answers2

0

According to regex101.com, this works:

/\/(op|kl|xy)/g

With the long version being:

new RegExp('\\/(op|kl|xy)', 'g')
David
  • 3,552
  • 1
  • 13
  • 24
-1

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

Samuel
  • 1,128
  • 4
  • 14
  • 34