Skip to content

Commit 1c50354

Browse files
committed
Readme tweaks
1 parent 420bdd4 commit 1c50354

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

README.md

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,11 @@ re.exec('test aaaaaabbb')[0];
5656

5757
#### As the entire string
5858

59+
Use `\g<name&R=N>` to recursively match just the specified group.
60+
5961
```js
6062
const re = regex({plugins: [recursion]})`
61-
^
62-
(?<balanced>
63-
a
64-
# Recursively match just the specified group
65-
\g<balanced&R=50>?
66-
b
67-
)
68-
$
63+
^ (?<r> a \g<r&R=50>? b) $
6964
`;
7065
re.test('aaabbb'); // → true
7166
re.test('aaabb'); // → false
@@ -76,7 +71,7 @@ re.test('aaabb'); // → false
7671
```js
7772
// Matches all balanced parentheses up to depth 50
7873
const parens = regex({flags: 'g', plugins: [recursion]})`
79-
\( ( [^\(\)] | (?R=50) )* \)
74+
\( ([^\(\)] | (?R=50))* \)
8075
`;
8176

8277
'test ) (balanced ((parens))) () ((a)) ( (b)'.match(parens);
@@ -92,40 +87,40 @@ Following is an alternative that matches the same strings, but adds a nested qua
9287

9388
```js
9489
const parens = regex({flags: 'g', plugins: [recursion]})`
95-
\( ( (?> [^\(\)]+ ) | (?R=50) )* \)
90+
\( ((?> [^\(\)]+) | (?R=50))* \)
9691
`;
9792
```
9893

99-
This matches sequences of non-parens 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 parens.
94+
This 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.
10095

101-
[Atomic groups](https://github.com/slevithan/regex#atomic-groups) are provided by the base `regex` library.
96+
[Atomic groups](https://github.com/slevithan/regex#atomic-groups) are provided by the base Regex+ library.
10297

10398
### Match palindromes
10499

105100
#### Match palindromes anywhere within a string
106101

107102
```js
108103
const palindromes = regex({flags: 'gi', plugins: [recursion]})`
109-
(?<char> \w )
104+
(?<char> \w)
110105
# Recurse, or match a lone unbalanced char in the middle
111-
( (?R=15) | \w? )
106+
((?R=15) | \w?)
112107
\k<char>
113108
`;
114109

115110
'Racecar, ABBA, and redivided'.match(palindromes);
116111
// → ['Racecar', 'ABBA', 'edivide']
117112
```
118113

119-
In the example above, the max length of matched palindromes is 31. That's because it sets the max recursion depth to 15 with `(?R=15)`. So, depth 15 × 2 chars (left + right) for each depth level + 1 optional unbalanced char in the middle = 31. To match longer palindromes, the max recursion depth can be increased to a max of 100, which would enable matching palindromes up to 201 characters long.
114+
Palindromes are sequences that read the same backwards as forwards. In the example above, the max length of matched palindromes is 31. That's because it sets the max recursion depth to 15 with `(?R=15)`. So, depth 15 × 2 chars (left + right) for each depth level + 1 optional unbalanced char in the middle = 31. To match longer palindromes, the max recursion depth can be increased to a max of 100, which would enable matching palindromes up to 201 characters long.
120115

121116
#### Match palindromes as complete words
122117

123118
```js
124119
const palindromeWords = regex({flags: 'gi', plugins: [recursion]})`
125120
\b
126121
(?<palindrome>
127-
(?<char> \w )
128-
( \g<palindrome&R=15> | \w? )
122+
(?<char> \w)
123+
(\g<palindrome&R=15> | \w?)
129124
\k<char>
130125
)
131126
\b

0 commit comments

Comments
 (0)