tc39 / proposal-regexp-atomic-operators

https://rbuckton.github.io/proposal-regexp-atomic-operators
BSD 3-Clause "New" or "Revised" License
16 stars 1 forks source link

JS library with support for atomic groups #4

Open slevithan opened 4 months ago

slevithan commented 4 months ago

I added full support for atomic groups and possessive quantifiers to the regex library. It emulates them using lookahead and backreferences.

import {regex} from 'regex';

// Atomic group
regex`^(?>\w+\s?)+$`;

// Possessive quantifier
regex`a++.`;

Sharing here in case it's helpful for testing their behavior in a working JS library.

This also addresses #2.

Edit: @pygy's compose-regexp.js is the only other JS library I'm aware of that emulates atomic groups, but it uses a different approach with function composition rather than using the atomic group syntax, as shown below:

import {atomic, sequence, suffix} from 'compose-regexp';

// Atomic group
sequence(/^/, suffix('+', atomic(/\w+\s?/)), /$/);