-2

I want to use a regular expression to match strings of the form exclude=1 where the number should be in the range of 1 to 15.

I've tried: str.match("/exclude\=[1-9]|exclude\=1[0-5]/")

but it does not work.

Thank you!

Emelie
  • 31
  • 4

1 Answers1

1

You took a string,

str.match("/exclude\=[1-9]|exclude\=1[0-5]/")
          ^                                ^

but you need to remove the quotes for a regular expression and take a start and end sign into the expression.

const test = s => s.match(/^exclude=([1-9]|1[0-5])$/);

console.log(['', 'exclude=1', 'exclude=15', 'exclude=153'].map(test));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392