[thelist] Error in regular expression on IE for Mac

Liorean Liorean at user.bip.net
Tue Feb 5 10:33:01 CST 2002


At 11:48 2002-02-05 +0100, Allan Lund Jensen wrote:
>Using a regular expression I want to replace all occurences of 3 or more
>identical characters following each other with, for example, "x".
>So, the string:
>"1442455556333"
>would after doing a replace look like this:
>"14424x6x"
>
>My expression looks like this:
>var re                  = /(.)\1\1+/g;
>
>This causes IE to report an error ( "Unexpected quantifier" ). I think it
>stumbles on the "+".
>The above expression only fails in IE for Mac, and not using Windows or
>Netscape 6.
>
>To be fair I also tried using "new RegExp" ( and thus removing the g flag ).
>I think it looked a bit like this:
>var re = new RegExp( "(.)\\1\\1+" , "g" );
>(Im not sure this is what I ended up with, as I'm writing from home, not
>work ).
>In any case the above also worked in IE for Windows and Netscape 6, but not
>IE for Mac.
>
>So I guess the question buried in this mess is actually 2 questions:
>1) Is there another way to write my expression that IE for Mac will accept.
>2) Is the expression correct?
>
>Now, I am aware that there are other way to achieve the wanted result (using
>a loop ), but unless I'm mistaken, those other solutions would be longer,
>and since size is a factor, I really need to do this using regular
>expressions.

Try to encapsulate the backreference in parentheses, and use a quantifier
on that:

var re=/(.)\1(\1)+/g; // Alternatively /(.)(\1){2,}/g, but that's one
character longer
var str="1442455556333"; // Your sample string, just for functionality testing
var result=str.replace(re,'x'); // Makes result=="14424x6x"

Tested in NN4.79, IE6, Op6, Moz 0.9.7, and all gave the expected result.
Can't test on a mac though, since I lack one.

// Liorean




More information about the thelist mailing list