Regex101 Vs Javascript String.match Disagreement
This regex /{(\w+)}/g is supposed to match every word character between the curly braces {}. Instead, I'm getting different results in the Regex101 JavaScript engine and Chrome con
Solution 1:
The String.prototype.match
function returns an array of all matches when the 'g' flag is added to the pattern.
You want to use RegExp.prototype.exec
function to get the capture groups:
The exec() saves the last index of the match it found in the RegExp object and continues from there when you run it the next time. So, you need to loop over your string until the function returns null to get all the matches.
var str = "this {is} a {word} {test}";
var re = /{(\w+)}/g;
do{
var res = re.exec(str);
console.log(res);
} while( res );
Solution 2:
You could try using a negative lookahead followed by your pattern, then a positive lookahead at the end:
(\w+)(?=})
Resulting in this within the console:
"{asd}{asd2}".match(/(\w+)(?=})/g);
Example:
https://regex101.com/r/uL9rN0/1
Output:
Array [ "asd", "asd2" ]
Post a Comment for "Regex101 Vs Javascript String.match Disagreement"