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

  • Home
  • SEARCH
  • 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 8979971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:58:06+00:00 2026-06-15T19:58:06+00:00

I am seeing a weird behaviour when executing the same regexp matching several times:

  • 0

I am seeing a weird behaviour when executing the same regexp matching several times:

var r = /(.*)/g
var d = "a"

console.log(r.exec(d))
console.log(r.exec(d))

This produces:

["a", "a"]
["", ""]

Why is it not matching anything the second time around?

  • 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-15T19:58:07+00:00Added an answer on June 15, 2026 at 7:58 pm

    That is what the g flag does. When you use it, exec will continue the next search from the end the previous match. But after your first match (a) there is nothing more left in the string, so you get an empty match. This empty match is usually used to terminate an exec-loop. If you know that there is only one match, remove the g (it means “global” search).

    Note that you can (and should) get rid of those parentheses. They just cost you performance. Without them you will only get one a in the resulting array.

    If you do want to consider multiple matches, but disregard that last empty match, use the loop technique:

    var match;
    while(match = r.exec(d))
        // process match[0] here
    

    Note that you only need this loop if you actually have (meaningful) capturing groups. If not (if you only want to get full matches), you can use match instead as elclanrs points out:

    var matches = d.match(r);
    

    EDIT:

    I just realised, most of what I said is partially true but not the actual cause of ["", ""]. What really happens is this: the first time .* matches a. The second time the engine tries to continue the search after the previous match (after a). Since your pattern has .* (which mean 0 or more characters) it will now continue to match empty strings (because they match the pattern). And matching an empty string also does not advance the position for the next search. Hence, even with .match you will get ["a", ""] (match is clever enough to abort in such a case).

    In fact, if you use that regex with the loop-technique you will get an endless loop (because match = ["", ""] will obviously not cause the loop to terminate). But in any case, you should realise that your pattern is nonsensical due to the *. It can match anything (including nothing). At least use .+. For whatever purpose.

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

Sidebar

Related Questions

I am seeing some weird behaviour in my Backbone view. When i console.log this.model
I'm seeing some weird behaviour that I wasn't expecting. On a pure white matrix
I'm seeing some weird behaviour debugging my C# code - I cant post my
I'm seeing this weird behavior on Ember objects where when I alter an array
I'm seeing a really weird issue with this code and sample execution: https://gist.github.com/720278 The
We are using the image resizer from imageresizing.net and are seeing some weird behaviour.
I'm seeing weird behavior on this code: images = dict(cover=[],second_row=[],additional_rows=[]) for pic in pictures:
I'm seeing a weird behaviour in WPF with multiple instances of a UserControl. I'm
I'm seeing a weird error message and am looking for some ideas as to
I'm seeing some weird stuff happening in my app. After coming back to the

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.