Giter Site home page Giter Site logo

Comments (5)

gskinner avatar gskinner commented on August 19, 2024

This is correct behaviour.

A zero width match will match at the same position infinitely. For example, the pattern "z?" matching against the string "aaa", will match at position 0 infinitely. Because it matches 0 characters, it can satisfy the match without moving the search index, causing it to match at the same position again.

from regexr.

bkiers avatar bkiers commented on August 19, 2024

A zero width match will match at the same position infinitely.

Well, that depends on how you match against the target string. For example, the following:

"aaa".match(/(?=a)/g);

will return an array containing 3 empty strings.

But version 1 of Regexr did not behave this way. Is this also a change because of the use of the JavaScript regex engine?

from regexr.

gskinner avatar gskinner commented on August 19, 2024

This is true, however if you use the same example with .exec() you'll see that lastIndex never advances past 0.

var regex = /(?=a)/g;
regex.exec("aaa");
regex.lastIndex; // 0

The "infinite" error is driven from this condition. Specifically, if the lastIndex is identical for two executions, then we surface the infinite error.

I have to assume that .match automatically advances the index. Warrants some testing.

Version 1 behaves the same. If you plug that pattern in, you'll see it changes to red to indicate the same error - it's just less explicit about it. The main difference is that v1 will show any matches up to the infinite loop, whereas v2 won't. I thought it made more sense to show no matches in an error condition, to draw attention to the error (vs users winding up in infinite loops).

from regexr.

bkiers avatar bkiers commented on August 19, 2024

The main difference is that v1 will show any matches up to the infinite loop, whereas v2 won't.

Ah, that's what the red color means in v1 :)

Odd how JavaScript does not advance when the pattern is zero-width: most regex flavors I'm familiar with do so. Ah well, too bad.

Thanks for the info!

from regexr.

connec avatar connec commented on August 19, 2024

A 0-length regex is perfectly valid and can very useful. Iterating through the matches is also not difficult:

// Find positions for commas in a number (e.g., 12,345,678 -> 2, 5)
var s  = '12345678'
  , re = /\B(?=(?:\d{3})+(?!\d))/g
  , m;
while(m = re.exec(s)) {
  console.log('match at', m.index);
  if(m[0].length == 0 && ++re.lastIndex > s.length)
    break;
}

I've made a pull request that implements this, along with some naive highlighting.

from regexr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.