-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
  • Pass a string to RegExp constructor, see what the [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Description) say ... – Teemu Nov 16 '15 at 19:39
  • for example balicekt123@gmail.com but there can be what ever first name or last name – balicekt Nov 16 '15 at 19:41
  • Did you mean `/bal/i` as a working regex? – anubhava Nov 16 '15 at 19:44
  • yes exactly /bal/i is working for me but I need to change word "bal" for something what will be dynamic what will come to the function like a parameter – balicekt Nov 16 '15 at 19:45

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.