-1

I'm looking for a working solution with dynamic regex in Javascript.

this solution works for me: (but is not dynamic)

new RegExp(\bal\i);

but this solution is not working:

var value = 'bal';
new RegExp('\'+value+'\i');

Could anyone help me how to adjust it to make it work? Thank you

balicekt
  • 615
  • 5
  • 14

1 Answers1

2

you can pass the string (value) in the RegExp constructor, along with the ignoreCase flag as:

 var value = 'bal';
 var b = new RegExp(value, 'i')
 b.test('BAL')

it returns true.