#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    Brasilia, Brazil
    Posts
    2
    Rep Power
    0

    Question Regex exec() not working in IE8


    Internet Explorer 8 does not behave the same as Firefox and Chrome for regex exec() method in javascript. IE8 seems not to comply with ECMA-262 here, as the others do. Regex's match() method works OK for all.

    Any ideas on how to work around to use exec() with IE8?

    Here is the sample code to show the issue:

    <body>
    <script type="text/javascript">
    var str = "images/1t.png";
    var re1=/.*\//g;
    var withMatch = str.match(re1);
    var withExec = re1.exec(str);
    document.write("withMatch=" + withMatch+"<br />");
    document.write("withExec=" + withExec);
    </script>
    </body>

    Results:

    IE8: withMatch=images/ withExec=null
    Firefox and Chrome withMatch=images/ withExec=images/
  2. #2
  3. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,008
    Rep Power
    2790
    Why do you want to use exec()? It is extremely heavy on computation and can be dangerous.
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  4. #3
  5. No Profile Picture
    JackOfAllTrades, MasterOfNone
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    Kentucky, USA
    Posts
    398
    Rep Power
    124
    Originally Posted by Winters
    Why do you want to use exec()? It is extremely heavy on computation and can be dangerous.
    Say what? RegExp.exec() is actually less computationally intensive than String.match(), since the latter will find all matches on a global search. Also, I'm not sure how running a match for a regular expression could be dangerous. Are you thinking of eval() perhaps?


    Anyway, I can confirm this behavior in IE8.

    The problem seems to be that when running String.match() with a global RegExp (the g after the last /) it sets the RegExp's lastIndex property. This means that when you then call exec() on that RegExp, it is looking for the next match, but there isn't one. Thus, null.

    There are a couple of workarounds:
    • Don't call String.match() with the RegExp before calling exec()
    • Set the RegExp's lastIndex back to 0 after calling String.match()
    • Don't use a global match RegExp (it will only return the first match, which RegExp.exec() does anyway until you call it again)

    Firefox and Safari seem to leave lastIndex alone after String.match(), and I didn't test any other versions of IE.

    Comments on this post

    • Kravvitz agrees
    • Winters agrees : Yes, sigh.. long day. I was thinking of eval().
    Last edited by jnsg; July 29th, 2009 at 03:20 PM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    Brasilia, Brazil
    Posts
    2
    Rep Power
    0

    Thumbs up


    Originally Posted by jnsg
    Say what? RegExp.exec() is actually less computationally intensive than String.match(), since the latter will find all matches on a global search. Also, I'm not sure how running a match for a regular expression could be dangerous. Are you thinking of eval() perhaps?


    Anyway, I can confirm this behavior in IE8.

    The problem seems to be that when running String.match() with a global RegExp (the g after the last /) it sets the RegExp's lastIndex property. This means that when you then call exec() on that RegExp, it is looking for the next match, but there isn't one. Thus, null.

    There are a couple of workarounds:
    • Don't call String.match() with the RegExp before calling exec()
    • Set the RegExp's lastIndex back to 0 after calling String.match()
    • Don't use a global match RegExp (it will only return the first match, which RegExp.exec() does anyway until you call it again)

    Firefox and Safari seem to leave lastIndex alone after String.match(), and I didn't test any other versions of IE.
    Thanks, jnsg!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    1
    Rep Power
    0
    Thanks so much, setting lastIndex to 0 after the match() fixes it!

IMN logo majestic logo threadwatch logo seochat tools logo