Skip to content

Commit 6ecdb3c

Browse files
committed
Readme updates
1 parent ea41987 commit 6ecdb3c

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ This is an official plugin for [Regex+](https://github.com/slevithan/regex) that
99
> [!NOTE]
1010
> Regex flavors vary on whether they offer infinite or fixed-depth recursion. For example, recursion in Oniguruma uses a default depth limit of 20.
1111
12-
Recursive matching is added to a regex via one of the following (the recursion depth limit is provided in place of *`N`*):
12+
Recursive matching is added to a regex via the following syntax. The recursion depth limit is provided in place of *`N`*.
1313

1414
- `(?R=N)` — Recursively match the entire regex at this position.
15-
- `\g<name&R=N>` or `\g<number&R=N>` — Recursively match the contents of the group referenced by name or number at this position. The `\g` subroutine must be *within* the referenced group.
15+
- `\g<name&R=N>` or `\g<number&R=N>` — Recursively match the contents of the group referenced by name or number at this position. The `\g<…>` subroutine must be *within* the referenced group.
1616

17-
Multiple uses of recursion within the same pattern are allowed if they are non-overlapping. Named captures and backreferences are supported within recursion, and are independent per depth level. So e.g. `match.groups.name` is the value captured by group `name` at the top level of the recursion stack.
17+
Details:
18+
19+
- Multiple uses of recursion within the same pattern are supported if they're non-overlapping.
20+
- Named captures and backreferences are supported within recursion, and are independent per depth level. Given a result named `match`, `match.groups.name` is the value captured by group `name` at the top level of the recursion stack.
1821

1922
## 📜 Contents
2023

@@ -45,11 +48,11 @@ const {recursion} = require('regex-recursion-cjs');
4548
const re = regex({plugins: [recursion]})``;
4649
```
4750

48-
> **Note:** [`regex-recursion-cjs`](https://www.npmjs.com/package/regex-recursion-cjs) is a third-party CommonJS wrapper for this library. It might not always be up to date with the latest version.
51+
> **Note:** [*regex-recursion-cjs*](https://www.npmjs.com/package/regex-recursion-cjs) is a third-party CommonJS wrapper for this library. It might not always be up to date with the latest version.
4952
</details>
5053
5154
<details>
52-
<summary>Using a global name (no import)</summary>
55+
<summary>Using a global name in browsers</summary>
5356

5457
```html
5558
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/regex.min.js"></script>
@@ -108,21 +111,22 @@ const parens = regex({flags: 'g', plugins: [recursion]})`
108111
Following is an alternative that matches the same strings, but adds a nested quantifier. It then uses an atomic group to prevent the nested quantifier from creating the potential for [catastrophic backtracking](https://www.regular-expressions.info/catastrophic.html). Since the example above doesn't *need* a nested quantifier, this isn't an improvement but merely an alternative that shows how to deal with the general problem of nested quantifiers that create multiple ways to divide matches of the same strings.
109112

110113
```js
114+
// With an atomic group
111115
const parens = regex({flags: 'g', plugins: [recursion]})`
112116
\( ((?> [^\(\)]+) | (?R=20))* \)
113117
`;
114118

115-
// Or with a possessive quantifier
119+
// Same thing, but with a possessive quantifier
116120
const parens = regex({flags: 'g', plugins: [recursion]})`
117121
\( ([^\(\)]++ | (?R=20))* \)
118122
`;
119123
```
120124

121125
The first example above matches sequences of non-parentheses in one step with the nested `+` quantifier, and avoids backtracking into these sequences by wrapping it with an atomic group `(?>…)`. Given that what the nested quantifier `+` matches overlaps with what the outer group can match with its `*` quantifier, the atomic group is important here. It avoids exponential backtracking when matching long strings with unbalanced parentheses.
122126

123-
In cases where you're you're repeating a single token within an atomic group, possessive quantifiers provide syntax sugar.
127+
In cases where you're you're repeating a single token within an atomic group, possessive quantifiers (in this case, `++`) provide syntax sugar for the same behavior.
124128

125-
Atomic groups and possessive quantifiers are provided by the base Regex+ library.
129+
Atomic groups and possessive quantifiers are [provided](https://github.com/slevithan/regex#atomic-groups) by the base Regex+ library.
126130

127131
### Match palindromes
128132

@@ -159,7 +163,7 @@ const palindromeWords = regex({flags: 'gi', plugins: [recursion]})`
159163
// → ['Racecar', 'ABBA']
160164
```
161165

162-
## ✂️ Direct use, without Regex+
166+
## ⛓️‍💥 Direct use, without Regex+
163167

164168
```js
165169
import {recursion} from 'regex-recursion';
@@ -174,11 +178,11 @@ re.exec('foo (bar (baz) blah) end')[0];
174178
// → '(bar (baz) blah)'
175179
```
176180

177-
Because the generated pattern is used without Regex+, you can't include extended syntax like insignificant whitespace, atomic groups, possessive quantifiers, or non-recursive subroutines.
181+
Because the generated pattern is used without Regex+, you can't include Regex+'s extended syntax like insignificant whitespace, atomic groups, possessive quantifiers, or non-recursive subroutines.
178182

179183
## 🏷️ About
180184

181-
regex-recursion was created by [Steven Levithan](https://github.com/slevithan).
185+
Created by [Steven Levithan](https://github.com/slevithan).
182186

183187
If you want to support this project, I'd love your help by contributing improvements, sharing it with others, or [sponsoring](https://github.com/sponsors/slevithan) ongoing development.
184188

0 commit comments

Comments
 (0)