
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
regex-recursion
Advanced tools
This is a plugin for the 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.
Recursive matching is added to a regex via one of the following:
(?R=N)
ā Recursively match the entire regex at this position.\g<name&R=N>
ā Recursively match the contents of group name at this position.
\g
subroutine must be called within the referenced group.Recursive matching supports named captures/backreferences, and makes them independent per depth level. So e.g. groups.name
on a match object is the value captured by group name
at the top level of the recursion stack.
npm install regex regex-recursion
import {regex} from 'regex';
import {recursion} from 'regex-recursion';
const re = regex({plugins: [recursion]})`ā¦`;
In browsers (using a global name):
<script src="https://cdn.jsdelivr.net/npm/regex@4.0.0/dist/regex.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/regex-recursion@4.0.0/dist/regex-recursion.min.js"></script>
<script>
const {regex} = Regex;
const {recursion} = Regex.plugins;
const re = regex({plugins: [recursion]})`ā¦`;
</script>
// Matches sequences of up to 50 'a' chars followed by the same number of 'b'
const re = regex({plugins: [recursion]})`a(?R=50)?b`;
re.exec('test aaaaaabbb')[0];
// ā 'aaabbb'
const re = regex({plugins: [recursion]})`^
(?<balanced>
a
# Recursively match just the specified group
\g<balanced&R=50>?
b
)
$`;
re.test('aaabbb'); // ā true
re.test('aaabb'); // ā false
Note the ^
and $
anchors outside of the recursive subpattern.
// Matches all balanced parentheses up to depth 50
const parens = regex({flags: 'g', plugins: [recursion]})`\(
( [^\(\)] | (?R=50) )*
\)`;
'test ) (balanced ((parens))) () ((a)) ( (b)'.match(parens);
/* ā [
'(balanced ((parens)))',
'()',
'((a))',
'(b)'
] */
Following is 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 catastrophic backtracking.
const parens = regex({flags: 'g', plugins: [recursion]})`\(
( (?> [^\(\)]+ ) | (?R=50) )*
\)`;
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.
Atomic groups are provided by the base regex
library.
const palindromes = regex({flags: 'gi', plugins: [recursion]})`
(?<char> \w )
# Recurse, or match a lone unbalanced char in the middle
( (?R=15) | \w? )
\k<char>
`;
'Racecar, ABBA, and redivided'.match(palindromes);
// ā ['Racecar', 'ABBA', 'edivide']
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.
const palindromeWords = regex({flags: 'gi', plugins: [recursion]})`\b
(?<palindrome>
(?<char> \w )
( \g<palindrome&R=15> | \w? )
\k<char>
)
\b`;
'Racecar, ABBA, and redivided'.match(palindromeWords);
// ā ['Racecar', 'ABBA']
Note the \b
word boundaries outside of the recursive subpattern.
FAQs
Recursive matching plugin for Regex+
The npm package regex-recursion receives a total of 1,405,741 weekly downloads. As such, regex-recursion popularity was classified as popular.
We found that regex-recursion demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Ā It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.