Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1068461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:17:27+00:00 2026-05-16T20:17:27+00:00

I was experiencing some weird behaviour in some of my javascript code, but only

  • 0

I was experiencing some weird behaviour in some of my javascript code, but only in Firefox and Chrome. IE is fine.

I have isolated the problem and created a little page so you can see the behaviour yourself.

Essentially, it appears as if the Regular Expression object in MethodC is being reused across method calls to MethodC, even though it’s a local variable. Can someone explain this behaviour?

<html>
<head>
<script type="text/javascript">
    function RunDemo()
    {
        var subject = "01234 555 6789";

        for (var i = 1; i <= 10; i++) {
            MethodA(subject, i);
            MethodB(subject, i);
            MethodC(subject, i);
        }
    }

    // OK, OK, OK, OK, OK, OK, OK, OK, OK, OK
    function MethodA(subject, iteration)
    {
        var myRegexp = new RegExp("5", "g");
        var matches = myRegexp.exec(subject);
        AddItem(matches ? "OK" : "no match", "listA");
    }

    // OK, OK, OK, OK, OK, OK, OK, OK, OK, OK
    function MethodB(subject, iteration)
    {
        var myRegexp = /5/;
        var matches = myRegexp.exec(subject);
        AddItem(matches ? "OK" : "no match", "listB");
    }

    // OK, OK, OK, no match, OK, OK, OK, no match, OK, OK (in FireFox and Chrome, IE is fine)
    function MethodC(subject, iteration) {
        var myRegexp = /5/g;
        var matches = myRegexp.exec(subject);
        AddItem(matches ? "OK" : "no match", "listC");
    }

    function AddItem(itemText, listID) {
        var li = document.createElement("li");
        li.innerHTML = itemText;
        document.getElementById(listID).appendChild(li);
    }   

</script>
</head>
<body onload="RunDemo()">
    <h2>Method A</h2>    
    <ul id="listA"></ul>

    <h2>Method B</h2> 
    <ul id="listB"></ul>

    <h2>Method C</h2> 
    <ul id="listC"></ul>
</body>
</html>
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:17:28+00:00Added an answer on May 16, 2026 at 8:17 pm

    The optimizers in V8 and spidermonkey create a regex object when they see a regex literal and reuse it.

    Per ECMA3, this is compliant behavior, but it will become non-compliant in ECMA5.

    7.8.5 Regular Expression Literals

    A regular expression literal is an input element that is converted to a RegExp object (section 15.10) when it is
    scanned. The object is created before evaluation of the containing program or function begins. Evaluation of the
    literal produces a reference to that object; it does not create a new object. Two regular expression literals in a
    program evaluate to regular expression objects that never compare as === to each other even if the two literals’
    contents are identical. A RegExp object may also be created at runtime by new RegExp (section 15.10.4) or calling
    the RegExp constructor as a function (section 15.10.3).

    ECMAScript Language Specification Edition 3

    Compare to:

    7.8.5 Regular Expression Literals

    A regular expression literal is an input element that is converted to a RegExp object (see 15.10) each time the
    literal is evaluated. Two regular expression literals in a program evaluate to regular expression objects that
    never compare as === to each other even if the two literals’ contents are identical. A RegExp object may also
    be created at runtime by new RegExp (see 15.10.4) or calling the RegExp constructor as a function (15.10.3).

    ECMAScript Language Specification Edition 5

    Here are some workarounds:

    • Don’t use the /g flag with exec.
    • Create a RegExp from the RegExp consructor instead of from a regexp literal.

    Doing either or both of these should, I think, make the problem go away.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am experiencing some weird behaviour here. i am using the following code to
I am experiencing some problem on UTF-8 Encoding. I have a CSV file and
I am experiencing some weird encoding behaviour in my ASP.NET MVC project. In my
I'm experiencing some really weird object behaviours at the moment. I have this template
I am experiencing something weird and have a workaround already, but I don't think
I'm experiencing a weird behaviour with one Button in Android. I created a Custom
I am experiencing some weird behavior with an android app I have been working
I have a Nivo slide gallery and am experiencing some weird inconsistency between browsers.
I am experiencing some pretty weird output from some c code. Granted I am
I'm experiencing some weird behaviour when using a ASP.NET LinkButton with a OnClientClick-property. ASPX

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.