I’m trying JS regX to match some values and replace them with the markup. So here is the code I tried.
content = '<p>[dfp:advertisement:leaderboard:750x90]</p>text here<p>[dfp:advertisement:box1:300x250]</p>';
var regX = /([a-zA-Z0-9]*):([0-9]*x[0-9]*)/g;
match = regX.exec(content);
Okay, the issue is this will only match the first value, not the second one. I’ve added /g but no luck yet.
Thanks for looking.
A single call to
execwill only return the first match, even with thegflag. But the regex will contain state such that subsequent multiple calls toexecusing the same regex will return the following matching items.From the MDN
execdocumentation:So this code will
alertthe first two matches from your example:As others have already mentioned, you can instead use
matchon a string to return an array with the multiple matches.So, similar to the above code, this code will
alertthe first two matches from your example: