1

I have created a regular expression with a variable, which is dynamically passed.

new RegExp("(" + variable_name + ":\\((.*?)\\))");

It perfectly results in /(ABC:\((.*?)\))/ (if variable_name is ABC)

But, If I pass an option as

new RegExp("(" + variable_name + ":\\((.*?)\\))") + "g",

it gives me the regular expression in string format

"/(ABC:\((.*?)\))/g"

and the match() function fails. Is there any good way to pass option without converting the expression into string.

2
  • Thanks for the answer guys. Great answer to a silly question. :( Commented Oct 7, 2014 at 11:12
  • Don't have necessary reputation to upvote all answers. :-| Commented Oct 7, 2014 at 11:42

3 Answers 3

2

You need to include the g (global modifier) within the brackets.

new RegExp("(" + variable_name + ":\\((.*?)\\))", "g"); 
Sign up to request clarification or add additional context in comments.

Comments

2

use this :

new RegExp("(" + variable_name + ":\\((.*?)\\))", "g");

Comments

1

You can include whatever option you need:

var options = "g"; //g, m, i, or combination of them
new RegExp("(" + variable_name + ":\\((.*?)\\))", options);

Hope it's useful!

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.