Comments (5)
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.
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.
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.
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.
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)
- PCRE engine matches รค (ASCII char 228) to other diacritics
- Separator not working for list tool for PCRE patterns
- Issue with Regex Tool's Lack of Support for Lazy Quantifier "{,M}" Without Lower Limit HOT 2
- Site is down with "connection timed out, Error code 522" HOT 1
- Support for `v` (`unicodeSets`) and `d` (`hasIndices`) flags HOT 3
- Support for named backreferences `\k<...>`
- i love you, whoever made this.
- \n not working as expected HOT 1
- Global indicator not working in tests HOT 1
- When nearby matches are present, hard to see visually.
- full match not working in test
- can't build it? HOT 1
- Hide Text/Tests pane
- Explain PCRE modes like `(?i)` more
- Feature request: Paste lines to be used for tests HOT 2
- Inconsistent / broken pattern matching for unicode letters in PCRE mode
- Feature Request: Change default author
- Cannot delete Pattern created by '[anonymous]'
- [Feature Request] Import multiple tests using a file text or a csv list or json list...
- Chromium browsers cannot append text after space character HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from regexr.