12

I tried to rewrite the method (part of tutorial on w3schools).

The problem is to make a variable string to become part of the regular expression.

Tutorial Sample code:

function myFunction() {
    var str = "The rain in SPAIN stays mainly in the plain"; 
    var res = str.match(/ain/gi);
    console.log(res)
}

I tried:

function myFunction() {
    var str = "The rain in SPAIN stays mainly in the plain"; 
    var test = "ain";
    var re = "/"+test+"/gi";
    var res = str.match(re);
    console.log(res);
}

The way I tried did not work.

0

5 Answers 5

16

Use the regex constructor, like:

function myFunction() {
    var str = "The rain in SPAIN stays mainly in the plain",
        test = "ain",
        re = new RegExp(test, 'gi'),
        res = str.match(re);

    console.log(res);
}
Sign up to request clarification or add additional context in comments.

Comments

7

You need to use RegExp constructor if you want to pass a value of variable as regex.

var test = "ain";
var re = new RegExp(test, "gi");

If your variable contains special chars, it's better to escape those.

var re = new RegExp(test.replace(/(\W)/g, "\\$1"), "gi");

Comments

6

Note that you pass a string to match. If we follow the documentation, this will do a new RegExp(test). So you should avoid / and /gi strings and add corresponding flags to the RegExp constructor: the default constructor doesn't add neither global search (g) nor case insensitive search (i).

So the solution to your problem:

var str = "The rain in SPAIN stays mainly in the plain"; 
var test = "ain";
var res = str.match(new RegExp(test, "gi"));

This will returns :

Array [ "ain", "AIN", "ain", "ain" ]

Note :

The form str.match(test, "gi"); works only in Firefox browser but is deprecated and throws a console warning from Firefox 39 (see RGraham comment).

5 Comments

This won't pass the gi flags to the RegExp constructor, according to the linked docs. It passes g by default, but not i
@RGraham this is the reason why I pass "gi" to str.match. If we do not pass it, the result is just Array [ "ain" ] : no global search and no case insensitive search. So I would say by default nothing is passed to the RegExp constructor.
There is no overload for String.prototype.match which accepts 2 arguments. At least in the spec & in Chrome implementation. Your code results in Array [ "ain" ] on Chrome 42
Good point. I tested in FF 31 and the overload is present and the result is the one I provided. I will update my answer.
Yeah, this is mentioned in the Firefox-specific notes on MDN, but a warning only appears on FF39 onwards
5

Yes match() not works with string literals

You can use var re = new RegExp(test,"gi");

 var str = "The rain in SPAIN stays mainly in the plain"; 
    var test = "ain";
    var re = new RegExp(test,"gi");
    var res = str.match(re);
    console.log(res);

Comments

5

You could have searched for "dynamic regular expressions" and you would have found: Javascript Regexp dynamic generation from variables? or Use dynamic (variable) string as regex pattern in JavaScript which both describe it very well.

3 Comments

That really should be a comment, not an answer. Or perhaps this question should be marked as a duplicate of one of the linked answers?
I have no idea how this could be done. If you tell me, I will do so for the next duplicates that I see. But most of the times when I saw other duplicates, people responded as "answer" as it was the answer to the problem.
@eX0du5 You may have seen it, but that's not how it works :) If you have enough rep, you can see a "Close" option. If you don't, leave a comment explaining it's a duplicate & someone with enough rep will vote to close for you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.