2

Can i append regex literal with string literal in javascript ? Like this:

var str = "iam 91 old";
var query = 'old';
var reg = /(\d+\s + query +)/;
alert(reg.exec(str)[1]);

this cod will be error.

1 Answer 1

3

Don't use a literal.

Use the RegExp constructor :

var reg = new RegExp("(\\d+\\s" + query + ")");
Sign up to request clarification or add additional context in comments.

2 Comments

can i call this reg.exec() function in this way?
Yes, it's exactly the same as /(\d+\sold)/

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.