full site update
This commit is contained in:
29
node_modules/regex/README.md
generated
vendored
29
node_modules/regex/README.md
generated
vendored
@@ -64,6 +64,7 @@ With the Regex+ library, JavaScript steps up as one of the best regex flavors al
|
||||
- Atomic groups and possessive quantifiers can dramatically improve performance and prevent ReDoS.
|
||||
- Subroutines and definition groups enable powerful composition, improving readability and maintainability.
|
||||
- Recursive matching via an official plugin.
|
||||
- Custom syntax plugins supported.
|
||||
|
||||
**Context-aware and safe interpolation** of regexes, strings, and partial patterns.
|
||||
|
||||
@@ -148,7 +149,10 @@ regex`^ (?<first>.) ${double} ${double} $`;
|
||||
// → /^(?<first>.)(.)\2(.)\3$/v
|
||||
```
|
||||
|
||||
See also this example of using a subroutine definition group to [refactor an IP address regex for readability](https://x.com/slevithan/status/1828112006353953055).
|
||||
See also the following examples of using subroutine definition groups to refactor regexes for readability and maintainability:
|
||||
|
||||
- [IP address regex](https://x.com/slevithan/status/1828112006353953055).
|
||||
- [Date time regex](https://bsky.app/profile/slev.life/post/3lgc6ullyvk2x).
|
||||
|
||||
## ❓ Context
|
||||
|
||||
@@ -318,7 +322,7 @@ Above, the `{0}` quantifier at the end of the `(?<byte>…)` group allows *defin
|
||||
This next regex matches a record with multiple date fields, and captures each value:
|
||||
|
||||
```js
|
||||
const record = regex`
|
||||
const re = regex`
|
||||
^ Admitted:\ (?<admitted> \g<date>) \n
|
||||
Released:\ (?<released> \g<date>) $
|
||||
|
||||
@@ -359,9 +363,8 @@ const re = regex`
|
||||
(?<day> \d{2})
|
||||
)
|
||||
`;
|
||||
|
||||
const record = 'Admitted: 2024-01-01\nReleased: 2024-01-03';
|
||||
const match = record.match(re);
|
||||
const match = re.exec(record);
|
||||
console.log(match.groups);
|
||||
/* → {
|
||||
admitted: '2024-01-01',
|
||||
@@ -470,7 +473,7 @@ regex`\b(ab|cd)\b`
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
> Flag <kbd>n</kbd> is based on .NET, C++, Oniguruma, PCRE, Perl, and XRegExp. It's not always specified by a flag, but where it can be (.NET, PCRE, Perl, XRegExp) it's always <kbd>n</kbd>. The option is variously called *explicit capture*, *no auto capture*, *don't capture group*, or *nosubs*. In Regex+, flag <kbd>n</kbd> also prevents using numbered backreferences to refer to named groups, which follows the behavior of C++ and the default handling of Oniguruma and Ruby. Referring to named groups by number is a footgun, and the way that named groups are numbered is inconsistent across regex flavors.
|
||||
> Flag <kbd>n</kbd> is based on .NET, C++, Oniguruma, PCRE, Perl, and XRegExp. Most of these share the flag letter <kbd>n</kbd>, but the option is variously called *explicit capture*, *no auto capture*, *don't capture group*, or *nosubs*. In Regex+, flag <kbd>n</kbd> also prevents using numbered backreferences to named groups, which follows the behavior of C++ and the default handling of Oniguruma and Ruby. Referring to named groups by number is a footgun, and the way that named groups are numbered is inconsistent across regex flavors.
|
||||
|
||||
## 🧩 Interpolation
|
||||
|
||||
@@ -737,13 +740,13 @@ regex({
|
||||
|
||||
**`flags`** — For providing flags when using an options object.
|
||||
|
||||
**`subclass`** — When `true`, the resulting regex is constructed using a `RegExp` subclass that avoids edge case issues with numbered backreferences. Without subclassing, submatches referenced *by number* from outside of the regex (e.g. in replacement strings) might reference the wrong values, because `regex`'s emulation of extended syntax (including atomic groups and subroutines) can add unnamed captures to generated regex source that might affect group numbering.
|
||||
**`subclass`** — When `true`, the resulting regex is constructed using a `RegExp` subclass that avoids edge case issues with numbered backreferences. Without subclassing, submatches referenced *by number* from outside of the regex (e.g. in replacement strings) might reference the wrong values, because `regex`'s emulation of extended syntax might add unnamed captures to generated regex source that can affect group numbering.
|
||||
|
||||
Context: `regex`'s implicit flag <kbd>n</kbd> (*named capture only* mode) means that all captures have names, so normally there's no need to reference submatches by number. In fact, flag <kbd>n</kbd> *prevents* you from doing so within the regex. And even in edge cases (such as when interpolating `RegExp` instances with numbered backreferences, or when flag <kbd>n</kbd> is explicitly disabled), any numbered backreferences within the regex are automatically adjusted to work correctly. However, issues can arise if you reference submatches by number (instead of their group names) from outside of the regex. Setting `subclass: true` resolves this, since the subclass knows about added "emulation groups" and automatically adjusts match results in all contexts.
|
||||
Context: `regex`'s implicit flag <kbd>n</kbd> (*named capture only* mode) means that all captures have names, so normally there's no need to reference submatches by number. In fact, flag <kbd>n</kbd> *prevents* you from doing so within the regex. And even in edge cases (such as when interpolating `RegExp` instances with numbered backreferences, or when flag <kbd>n</kbd> is explicitly disabled), any numbered backreferences within the regex are automatically adjusted to work correctly. However, issues can arise if you reference submatches by number (instead of their group names) from outside of the regex. Setting `subclass: true` resolves this, since the subclass knows about captures that are added only to emulate extended syntax, and automatically adjusts match results in all contexts.
|
||||
|
||||
> This option isn't enabled by default because it would prevent Regex+'s Babel plugin from emitting regex literals. It also has a small performance cost, and is rarely needed. The primary use case is tools that use `regex` internally with flag <kbd>n</kbd> disabled.
|
||||
> This option isn't enabled by default because it would prevent Regex+'s Babel plugin from emitting regex literals. It also has a tiny performance cost, and is rarely needed. The primary use case is tools that use `regex` internally with flag <kbd>n</kbd> disabled.
|
||||
|
||||
**`plugins`** — An array of functions. Plugins are called in order, after applying emulated flags and interpolation, but before the built-in plugins for extended syntax. This means that plugins can output extended syntax like atomic groups and subroutines. Plugins are expected to return an updated pattern string, and are called with two arguments:
|
||||
**`plugins`** — An array of functions. Plugins are called in order, after applying emulated flags and interpolation, but before the built-in plugins for extended syntax. This means that plugins can output extended syntax like atomic groups and subroutines. Plugins are expected to return an object with a string property `pattern`, and are called with two arguments:
|
||||
|
||||
1. The pattern, as processed so far by preceding plugins, etc.
|
||||
2. An object with a `flags` property that includes the native (non-emulated) flags that will be used by the regex.
|
||||
@@ -761,7 +764,7 @@ The final result after running all plugins is provided to the `RegExp` construct
|
||||
**`disable`** — A set of options that can be individually disabled by setting their values to `true`.
|
||||
|
||||
- **`x`** — Disables implicit, emulated [flag <kbd>x</kbd>](#flag-x).
|
||||
- **`n`** — Disables implicit, emulated [flag <kbd>n</kbd>](#flag-n). Note that, although it's safe to use unnamed captures and numbered backreferences within a regex when flag <kbd>n</kbd> is disabled, referencing submatches by number from *outside* a regex (e.g. in replacement strings) can result in incorrect values because extended syntax (atomic groups and subroutines) might add "emulation groups" to generated regex source. It's therefore recommended to enable the `subclass` option when disabling `n`.
|
||||
- **`n`** — Disables implicit, emulated [flag <kbd>n</kbd>](#flag-n). Note that, although it's safe to use unnamed captures and numbered backreferences within a regex when flag <kbd>n</kbd> is disabled, referencing submatches by number from *outside* a regex (e.g. in replacement strings) can result in incorrect values because extended syntax might add additional unnamed captures to generated regex source. It's therefore recommended to enable the `subclass` option when disabling `n`.
|
||||
- **`v`** — Disables implicit [flag <kbd>v</kbd>](#flag-v) even when it's supported natively, resulting in flag <kbd>u</kbd> being added instead (in combination with the `unicodeSetsPlugin`).
|
||||
- **`atomic`** — Disables [atomic groups](#atomic-groups) and [possessive quantifiers](#possessive-quantifiers), resulting in a syntax error if they're used.
|
||||
- **`subroutines`** — Disables [subroutines](#subroutines) and [subroutine definition groups](#subroutine-definition-groups), resulting in a syntax error if they're used.
|
||||
@@ -773,15 +776,15 @@ The final result after running all plugins is provided to the `RegExp` construct
|
||||
|
||||
### Returning a string
|
||||
|
||||
Function `rewrite` returns an object with properties `expression` and `flags` as strings, rather than returning a `RegExp` instance. This can be useful when you want to apply postprocessing to the output.
|
||||
Function `rewrite` returns an object with properties `pattern` and `flags` as strings, rather than returning a `RegExp` instance. This can be useful when you want to apply postprocessing to the output.
|
||||
|
||||
```js
|
||||
import {rewrite} from 'regex';
|
||||
rewrite('^ (ab | cd)', {flags: 'm'});
|
||||
// → {expression: '^(?:ab|cd)', flags: 'mv'}
|
||||
// → {pattern: '^(?:ab|cd)', flags: 'mv'}
|
||||
```
|
||||
|
||||
`rewrite` shares all of `regex`'s options (described above) except `subclass`. Providing the resulting `expression` and `flags` to the `RegExp` constructor produces the same result as using the `regex` tag.
|
||||
`rewrite` shares all of `regex`'s options (described above) except `subclass`. Providing the resulting `pattern` and `flags` to the `RegExp` constructor produces the same result as using the `regex` tag.
|
||||
|
||||
> Since `rewrite` isn't a template tag, it doesn't provide context-aware interpolation and doesn't automatically handle input as a raw string (you need to escape your backslashes).
|
||||
|
||||
|
Reference in New Issue
Block a user