views:

44

answers:

3

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]);.

+2  A: 

Perhaps I'm misunderstanding the question, but it seems like the Javascript implementation of the {n} operators is pretty good:

"foobar".match(/o{2,4}/); // => matches 'oo'
"fooobar".match(/o{2,4}/); // => matches 'ooo'
"foooobar".match(/o{2,4}/); // => matches 'oooo'
"fooooooobar".match(/o{2,4}/); // => matches 'oooo'
"fooooooobar".match(/o{2,4}?/); // => lazy, matches 'oo'
"foooobar".match(/(oo){2}/); // => matches 'oooo', and captures 'oo'
"fobar".match(/[^o](o{2,3})[^o]/); // => no match
"foobar".match(/[^o](o{2,3})[^o]/); // => matches 'foob' and captures 'oo'
"fooobar".match(/[^o](o{2,3})[^o]/); // => matches 'fooob' and captures 'oo'
"foooobar".match(/[^o](o{2,3})[^o]/); // => no match
Daniel Mendel
Unfortunately, this answer is irrelevant to my question, since my question was specifically "why isn't this feature working in my case?" -- it would be a good answer if the question were "does this work?" or "does JS support this type of regex?"
klanni
My apologies, when a question is asked without specific implementation details then I assume it must be about a feature generally. When you say `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.` then it seems like you're questioning the JavaScript support itself.
Daniel Mendel
Well, I said that hoping to point out the fact that I wasn't questioning whether it was supported in JS, I guess it backfired. ;)
klanni
Since we have no idea what your case is, I do not think you will ever get a satisfactoriy answer to your question. Provide some code, provide all the code that you have tried, and it is likely that someone will see something you missed. As it stands, the best answer one could give is, "The feature is supported, so you must be doing something wrong".
MooGoo
If you read the question completely, including the comments directly following it, all is explained. If there is something you are failing to understand about the issue, please ask and I can clarify. I have already provided all of the information relating to this issue. Again, if you're having trouble understanding any of the information given, just ask.Posting comments like "you must be doing something wrong" is pointless, since that fact is well-established (as well as the fact that the feature is supported).
klanni
A: 

It works for me.

var regex = [/z{5,}/gi,/a{4,5}/gi];
var subst = ['ZZZZZ','AAAAA'];
var somevar = 'zzzzz aaaaa aaaaaaa zzzzzzzzzz aaazzzaaaaaa';
print(somevar);
for (var i=0; i<2; i++) {
    somevar = somevar.replace(regex[i], subst[i]);
}
print(somevar);

output:

zzzzz aaaaa aaaaaaa zzzzzzzzzz aaazzzaaaaaa
ZZZZZ AAAAA AAAAAaa ZZZZZ aaazzzAAAAAa

The constructor version works, too:

var regex = [new RegExp('z{5,}','gi'),new RegExp('a{4,5}','gi')];

See it in action on ideone.com.

Alan Moore
A: 

I think I've figured it out. I was building the array various ways to get it to work, but what I think made the difference was using single-quotes around the regex string, instead of leaving it open like [/z{5,}/,/t{7,9}/gi]. So when I did ['/z{5,}/','/t{7,9}/gi'] that seems to have fixed it. Even though, like in Alan's example, it does sometimes work fine without them. Just not in my case I guess.

klanni