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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:40:47+00:00 2026-05-10T22:40:47+00:00

Some Context From Javascript: The Definitive Guide : When regexp is a global regular

  • 0

Some Context

From Javascript: The Definitive Guide:

When regexp is a global regular expression, however, exec() behaves in a slightly more complex way. It begins searching string at the character position specified by the lastIndex preperty of regexp. When it finds a match, it sets lastIndex to the position of the first character after the match.

I think anyone who works with javascript RegExps on a regular basis will recognize this passage. However, I have found a strange behavior in this method.

The Problem

Consider the following code:

>> rx = /^(.*)$/mg  >> tx = 'foo\n\nbar'  >> rx.exec(tx) [foo,foo] >> rx.lastIndex 3 >> rx.exec(tx) [,] >> rx.lastIndex 4 >> rx.exec(tx) [,] >> rx.lastIndex 4 >> rx.exec(tx) [,] >> rx.lastIndex 4 

The RegExp seems to get stuck on the second line and doesn’t increment the lastIndex property. This seems to contradict The Rhino Book. If I set it myself as follows it continues and eventually returns null as expected but it seems like I shouldn’t have to.

>> rx.lastIndex = 5 5 >> rx.exec(tx) [bar,bar] >> rx.lastIndex 8 >> rx.exec(tx) null 

Conclusion

Obviously I can increment the lastIndex property any time the match is the empty string. However, being the inquisitive type, I want to know why it isn’t incremented by the exec method. Why isn’t it?

Notes

I have observed this behavior in Chrome and Firefox. It seems to happen only when there are adjacent newlines.

[edit]

Tomalak says below that changing the pattern to /^(.+)$/gm will cause the expression not to get stuck, but the blank line is ignored. Can this be altered to still match the line? Thanks for the answer Tomalak!

[edit]

Using the following pattern and using group 1 works for all strings I can think of. Thanks again to Tomalak.

/^(.*)((\r\n|\r|\n)|$)/gm 

[edit]

The previous pattern returns the blank line. However, if you don’t care about the blank lines, Tomalak gives the following solution, which I think is cleaner.

/^(.*)[\r\n]*/gm 

[edit]

Both of the previous two solutions get stuck on trailing newlines, so you have to either strip them or increment lastIndex manually.

[edit]

I found a great article detailing the cross browser issues with lastIndex over at Flagrant Badassery. Besides the awesome blog name, the article gave me a much more in depth understanding of the issue along with a good cross browser solution. The solution is as follows:

var rx = /^/gm,     tx = 'A\nB\nC',     m;  while(m = rx.exec(tx)){     if(!m[0].length && rx.lastIndex > m.index){         --rx.lastIndex;     }      foo();      if(!m[0].length){         ++rx.lastIndex;     } } 
  • 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. 2026-05-10T22:40:48+00:00Added an answer on May 10, 2026 at 10:40 pm

    The problem is that the dot in

    ^(.*)$ 

    does not match new line characters, but with your 'm' switch you make '^' and '$' anchor to new line characters. That means the ‘nothing’ between '\n' and '\n' can be matched successfully with '(.*)'.

    Since this match is of zero width, the lastIndex property cannot advance. Try:

    ^(.+)$ 

    EDIT: To match the blank lines as well, do this:

    ^(.*)\n?     // remove all \r characters beforehand 

    or

    ^(.*)(?:\r\n|\n\r|\n|\r)?  // all possible CR/LF combinations, but *once* at most 

    …and just go for match group 1.

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

Sidebar

Ask A Question

Stats

  • Questions 107k
  • Answers 107k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Three ways: POST, GET, or cookies. which you use depends… May 11, 2026 at 9:04 pm
  • Editorial Team
    Editorial Team added an answer value = 100*cos(2*pi*(numDays/25)) Or something like that. May 11, 2026 at 9:04 pm
  • Editorial Team
    Editorial Team added an answer Yes, you need static libraries to build a statically linked… May 11, 2026 at 9:04 pm

Related Questions

We have an external .js file that we want to include in a number
I have an asp.net webservice with a parameter of type datetime. I have noticed
Here's a very simple Prototype example. All it does is, on window load, an
I need to make an AJAX request from a website to a REST web

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.