117

I found several similar questions, but it did not help me. So I have this problem:

var xxx = "victoria";
var yyy = "i";
alert(xxx.match(yyy/g).length);

I don't know how to pass variable in match command. Please help. Thank you.

2
  • 1
    Exact duplicate of How do you pass a variable to a Regular Expression JavaScript?. Search more ;) Commented Apr 4, 2014 at 1:46
  • One more thing: If you are using a variable to construct an regexp, cares should be taken that the variable might contain regexp special characters. e.g. if you pass "c++", the regex compiler will complain SyntaxError: Invalid regular expression: /c++/: Nothing to repeat Commented Dec 19, 2016 at 3:37

7 Answers 7

245

Although the match function doesn't accept string literals as regex patterns, you can use the constructor of the RegExp object and pass that to the String.match function:

var re = new RegExp(yyy, 'g');
xxx.match(re);

Any flags you need (such as /g) can go into the second parameter.

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

1 Comment

+1, this is the preferred way, BTW, if the argument passed to the match method is not a RegExp object, internally the RegExp constructor will be invoked using that value, so you can use a string pattern, e.g.: "a123".match("\\d+")[0] === "123";
18

You have to use RegExp object if your pattern is string

var xxx = "victoria";
var yyy = "i";
var rgxp = new RegExp(yyy, "g");
alert(xxx.match(rgxp).length);

If pattern is not dynamic string:

var xxx = "victoria";
var yyy = /i/g;
alert(xxx.match(yyy).length);

1 Comment

You get a imaginary downvote for using w3schools instead of MDN! developer.mozilla.org/en/docs/Web/JavaScript/Reference/…
10

Example. To find number of vowels within the string

var word='Web Development Tutorial';
var vowels='[aeiou]'; 
var re = new RegExp(vowels, 'gi');
var arr = word.match(re);
document.write(arr.length);

Comments

9

For example:

let myString = "Hello World"
let myMatch = myString.match(/H.*/)
console.log(myMatch)

Or

let myString = "Hello World"
let myVariable = "H"
let myReg = new RegExp(myVariable + ".*")
let myMatch = myString.match(myReg)
console.log(myMatch)

1 Comment

it is helpful but when i use let myString = "Tata Nexon EV Tata" let myVariable = "Tata" let myReg = new RegExp("\b"+myVariable , "g") let myMatch = myString.match(myReg) console.log(myMatch) not working
0

for me anyways, it helps to see it used. just made this using the "re" example:

var analyte_data = 'sample-'+sample_id;
var storage_keys = $.jStorage.index();
var re = new RegExp( analyte_data,'g');  
for(i=0;i<storage_keys.length;i++) { 
    if(storage_keys[i].match(re)) {
        console.log(storage_keys[i]);
        var partnum = storage_keys[i].split('-')[2];
    }
}

Comments

0

Below is an example of a simple and effective way to mock 'like' operator in JS. enjoy!

const likePattern = '%%i%%%%%a';
const regexp = new RegExp(likePattern.replaceAll('%','.*'),"i");
console.log("victoria".match(regexp));

Comments

-5
xxx.match(yyy, 'g').length

1 Comment

This will work only in Firefox, and it isn't even documented. The specification states that the String.prototype.match method expects only one argument. Using the RegExp constructor, as @Chris suggests is the preferred way.

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.