rbuckton / proposal-regexp-features

Proposal to investigate additional language features for ECMAScript Regular Expressions
MIT License
20 stars 1 forks source link

What about a flag for overlapped results? #7

Open oleedd opened 2 months ago

slevithan commented 2 months ago

What are some example use cases?

oleedd commented 2 months ago

"caratraany".match(/\wa\w/og) // "car", "rat", "raa", "aan" ("o" for overlapped results)

slevithan commented 2 months ago

That's an example of its behavior. What about use cases, explaining when this would actually be useful?

It's not that hard to get this behavior already with custom code that uses a loop and sets lastIndex to the character after the last match, whenever a match is found. So the question is what would justify building this capability into the language. (My personal interest is as an author of regex libraries where such a capability could be added if it were actually substantially useful.)

oleedd commented 2 months ago

It is to find results independent of each other. For example, finding words inside a word. Some regex engines support this. So it is not a new idea. It would be much better to have a flag instead of writing code. And lastIndex is not available for match, which is mainly used.

oleedd commented 1 month ago

@rbuckton New feature suggestion.