I have some JavaScript that runs uses a replace with regular expressions to modify content on a page. I'm having a problem with a specific regex quantifier, though. All the documentation I've read (and I know it work in regex in other languages, too) says that JavaScript supports the {N}
, {N,}
and {N,N}
quantifiers. That is, you can specify a particular number of matches you want, or a range of matches. E.g. (zz){5,}
matches at least 10 z's in a row, and z{5,10}
would match any number of z's from 5 to 10, no more and no less.
The problem is, I can match an exact number (e.g. z{5}
) but not a range. The nearest I can figure is that it has something to do with the comma in the regex string, but I don't understand why and can't get around this. I have tried escaping the comma and even using the unicode hexidecimal string for comma (\u002C
), but to no avail.
To clear up any possible misunderstandings, and to address some of the questions asked in the comments, here is some additional information (also found in the comments): I have tried creating the array in all possible ways, including var = [/z{5,}/gi,/a{4,5}/gi];
, var = [new RegExp('z{5,}', 'gi'), new RegExp('a{4,5}', 'gi')];
, as well as var[0] = new RegExp('z{5,}'), 'gi');
, var[1] = /z{5,}/gi;
, etc. The array is used in a for-loop as somevar.replace(regex[i], subst[i]);
.