Skip to content Skip to sidebar Skip to footer

Js: How To Match One Capture Group Multiple Times In The Same String?

The Situation I have a string that I want to match the same capture group multiple times. I need to be able to get at both matches. Example Code var curly = new RegExp('{([a-z0-9]+

Solution 1:

Try this:

var curly = /{([a-z0-9]+)}/gi,
        stringTest = "This is a {test} of my {pattern}",
        matched;
    while(matched = curly.exec(stringTest))
        console.log(matched);

Solution 2:

The exec method must be used in a while loop if you want to treat several results. See the MDN Documentation.

Solution 3:

Strings in JavaScript have a match method - you can use this to match all instances of a regular expression, returned as an array:

stringTest.match(curly);

This will return an array of matches:

> ["{test}", "{pattern}"]

To pull the inner values, you can use substr:

var arr = stringTest.match(curly);

arr[0] = arr[0].substr(1, arr[0].length - 2);
> "test"

Or to convert the entire array:

for (var i = 0; i < arr.length; i++)
    arr[i] = arr[i].substr(1, arr[i].length - 2);
> ["test", "pattern"]

Solution 4:

Normally this is done by the RegExp.exec method but you know it's convoluted. Everytime I need to use it i have to look up to remember this stateful operation. It's like you need a state monad or something :)

Anyways. In recent JS engines there is this String.matchAll() which does the same thing in a much sane fashion by returning an iterator object.

var curly = newRegExp("{([a-z0-9]+)}", "gi");
var stringTest = "This is a {test} of my {pattern}";

var matches = [...stringTest.matchAll(curly)];
console.log(matches);

If you need only the capture groups then just map the above result. Let's use Array.from() this time.

var curly = newRegExp("{([a-z0-9]+)}", "gi");
var stringTest = "This is a {test} of my {pattern}";

var matches = Array.from(stringTest.matchAll(curly), m => m[1]);
console.log(matches);

Post a Comment for "Js: How To Match One Capture Group Multiple Times In The Same String?"