proposal-regexp-features icon indicating copy to clipboard operation
proposal-regexp-features copied to clipboard

What about a flag for overlapped results?

Open oleedd opened this issue 1 year ago • 5 comments

oleedd avatar Sep 02 '24 10:09 oleedd

What are some example use cases?

slevithan avatar Sep 03 '24 07:09 slevithan

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

oleedd avatar Sep 03 '24 08:09 oleedd

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.)

slevithan avatar Sep 03 '24 08:09 slevithan

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 avatar Sep 03 '24 08:09 oleedd

@rbuckton New feature suggestion.

oleedd avatar Sep 05 '24 10:09 oleedd