0
var foo = "blaa";
var regex = /foo/i;

results in /foo/i instead of /blaa/i.

1 Answer 1

6

You can use the RegExp constructor:

var regex = new RegExp(foo, 'i');

It takes two arguments, the first expect a string that represents the regular expression pattern and in the second optional argument you can define the regular expression flags you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.