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 8208691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:27:53+00:00 2026-06-07T09:27:53+00:00

It is common knowledge that the exec method from RegExps can get used to

  • 0

It is common knowledge that the exec method from RegExps can get used to find all matches in a string. However, I just found out that if the regexp matches the empty string this loop can get stuck forever

var s = '1234'
var r = /()/g;
var m;
var i = 0;
while( (m = r.exec(s)) ){
   console.log(i, m[0]);
   if(++i >= 50){ console.log("infinite loop!"); break }
}

What is really weird though is that the plain string.match methods does not get stuck:

'1234'.match(/()/g) // Gives ["", "", "", "", "", ""]

I wonder how the match method is defined to work differently from the exec loop. So far the only way I found to avoid getting stuck like the match method does involves abusing the string.replace method, in a horrid hack:

var matches = [];
'1234'.replace(/()/g, function(m){ matches.push(m) });

So my question is:

How do the match and replace return a finite results when the regexp matches the empty string? Can I use the same technique to avoid getting stuck in the exec loop?

  • 1 1 Answer
  • 0 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-06-07T09:27:55+00:00Added an answer on June 7, 2026 at 9:27 am

    One (icky) solution is to make sure it’s not in the same place as last time:

    var s = '1234'
    var r = /()/g;
    var m;
    var i = 0;
    var lastPosition = -1;
    
    while(m = r.exec(s)) {
        if(m.index === lastPosition) {
            r.lastIndex++;
            continue;
        }
    
        lastPosition = m.index;
    
        console.log(i, m[0]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems to be common knowledge that hash tables can achieve O(1), but that
It is common knowledge that source filters are bad and should not be used
It is common knowledge ( and convention ) that namespaces are used to uniquely
It's common knowledge that using System.Diagnostics.Process.Start is the way to launch a url from
A common question that comes up from time to time in the world of
It's pretty common knowledge that JQuery is in both the Microsoft and Google CDN,
It appears to be common knowledge that interop with Excel and .Net is very
It is almost common knowledge that one should always compile with -Wall . What
It's common knowledge that C , F , L , l and M of
I know that this may be common knowledge, but is there a way to

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.