2
var str = 'TEST, STRING';
var regex = new RegExp('^(.*)('+str+')(.*)$/i');
console.log(regex);

Output

/^(.*)(TEST, STRING)(.*)$\/i/

But I need the following output:

/^(.*)(TEST, STRING)(.*)$\/i
1

1 Answer 1

4

The flags should be the second parameter to the RegExp constructor.

new RegExp('^(.*)(' + str + ')(.*)$', 'i');
                                    ^ ^^^

The syntax of RegExp constructor is

new RegExp(pattern[, flags])

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

4 Comments

Use ` RegExp('^(.*)('+str+')(.*)$\//i'); `
@ArifBurhan And that'll give /^(.*)(TEST, STRING)(.*)$\/\/i/
Only ` \\ ` can output ` \ `
@ArifBurhan You're not getting it. The first param to the RegExp constructor is the pattern. To add flags, use second param.

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.