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.

Mohitt
  • 2,957
  • 3
  • 29
  • 52

5 Answers5

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);
}
CD..
  • 72,281
  • 25
  • 154
  • 163
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");
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
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).

TrapII
  • 2,219
  • 1
  • 15
  • 15
  • This won't pass the `gi` flags to the `RegExp` constructor, according to the linked docs. It passes `g` by default, but not `i` – CodingIntrigue Jun 25 '15 at 07:11
  • @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. – TrapII Jun 25 '15 at 07:53
  • 1
    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 – CodingIntrigue Jun 25 '15 at 07:54
  • 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. – TrapII Jun 25 '15 at 07:57
  • Yeah, this is mentioned in the [Firefox-specific notes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match#Firefox-specific_notes) on MDN, but a warning only appears on FF39 onwards – CodingIntrigue Jun 25 '15 at 07:59
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);
squiroid
  • 13,809
  • 6
  • 47
  • 67
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.

Community
  • 1
  • 1
eX0du5
  • 896
  • 7
  • 16
  • 3
    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? – RobG Jun 25 '15 at 06:50
  • 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 Jun 25 '15 at 06:58
  • @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 – CodingIntrigue Jun 25 '15 at 07:07