You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a plugin for the [`regex`](https://github.com/slevithan/regex) library that adds support for recursive matching up to a specified max depth *N*, where *N* must be between 2 and 100. Generated regexes are native `RegExp` instances, and support all JavaScript regular expression features.
4
8
@@ -13,19 +17,26 @@ Recursive matching supports named captures/backreferences, and makes them indepe
Here's an alternative that matches the same strings, but adds a nested quantifier. It then uses an atomic group to prevent this nested quantifier from creating the potential for runaway backtracking:
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 runaway backtracking when matching long strings with unbalanced parens.
85
97
86
-
Atomic groups are provided by the base `regex`package.
98
+
Atomic groups are provided by the base `regex`library.
# Recurse, or match a lone unbalanced char in the middle
108
+
( (?R=15) | \w? )
109
+
\k<char>
110
+
`;
94
111
95
112
'Racecar, ABBA, and redivided'.match(palindromes);
96
113
// → ['Racecar', 'ABBA', 'edivide']
97
114
```
98
115
99
-
In this example, 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 center = 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.
116
+
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.
Template tag `rregex` is sugar for using the base `regex` tag and adding recursion support via a plugin. You can also add recursion support the verbose way:
0 commit comments