0

I want to build regular expression on the fly, like:

function buildReges(regex_string) {
   // here I want to use the string format to build a regex
   // like / + regex_string +/
}

I wonder how can I do that? Or if there is a way I can turn string into regular expression object?

1
  • @Amit THanks, Yes, this is JS, right now, what I can figure out is using JS Regex constructor, but I do not know how to specify /g /i Commented Oct 8, 2015 at 18:48

1 Answer 1

1

Use the RegExp constructor:

function buildReges(regex_string) {
   // here I want to use the string format to build a regex
   var re = new RegExp(regex_string);
}

if you want to use flags, you use a 2nd parameter:

new RegExp(regex_string, 'gi');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Yes, this is JS, right now, what I can figure out is using JS Regex constructor, but I do not know how to specify /g /i –
Thansk, this is helpful

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.