{"name":"regex-recursion","version":"4.3.0","description":"Recursive matching plugin for Regex+","author":{"name":"Steven Levithan"},"license":"MIT","type":"module","exports":{".":{"types":"./types/index.d.ts","import":"./src/index.js"}},"browser":"./dist/regex-recursion.min.js","types":"./types/index.d.ts","repository":{"type":"git","url":"git+https://github.com/slevithan/regex-recursion.git"},"keywords":["recursion","regex","regexp"],"dependencies":{"regex-utilities":"^2.3.0"},"devDependencies":{"esbuild":"^0.24.0","jasmine":"^5.4.0","regex":"^5.0.2","typescript":"^5.7.2"},"scripts":{"bundle:global":"esbuild src/index.js --global-name=Regex.plugins --bundle --minify --sourcemap --outfile=dist/regex-recursion.min.js","types":"tsc src/index.js --rootDir src --declaration --allowJs --emitDeclarationOnly --outDir types","prebuild":"rm -rf dist/* types/*","build":"pnpm run bundle:global && pnpm run types","pretest":"pnpm run build","test":"jasmine"},"_id":"regex-recursion@4.3.0","bugs":{"url":"https://github.com/slevithan/regex-recursion/issues"},"homepage":"https://github.com/slevithan/regex-recursion#readme","_integrity":"sha512-5LcLnizwjcQ2ALfOj95MjcatxyqF5RPySx9yT+PaXu3Gox2vyAtLDjHB8NTJLtMGkvyau6nI3CfpwFCjPUIs/A==","_resolved":"C:\\Users\\steve\\AppData\\Local\\Temp\\6b09a0882f434117799168d18d05e3fd\\regex-recursion-4.3.0.tgz","_from":"file:regex-recursion-4.3.0.tgz","_nodeVersion":"22.11.0","_npmVersion":"10.9.0","dist":{"integrity":"sha512-5LcLnizwjcQ2ALfOj95MjcatxyqF5RPySx9yT+PaXu3Gox2vyAtLDjHB8NTJLtMGkvyau6nI3CfpwFCjPUIs/A==","shasum":"32c3a42a57d78bf2d0c83875074c2f7ebaf2a4f6","tarball":"http://registry-npm.ywwl.com/regex-recursion/download/regex-recursion-4.3.0.tgz","fileCount":7,"unpackedSize":38386,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCW/Zw5MyioUwLk7ctePT7viLcnxRyA/XdsMQBmh0ZDZwIhAPjoUBxJh1P6Hfyek0TdOrgrg/gR1p+/JveUTwOPDFJ0"}],"size":11696,"noattachment":false,"key":"/regex-recursion/-/regex-recursion-4.3.0.tgz"},"_npmUser":{"name":"slevithan","email":"steves_list@hotmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/regex-recursion_4.3.0_1732710829456_0.8196165356265783"},"_hasShrinkwrap":false,"readme":"# regex-recursion\n\n[![npm version][npm-version-src]][npm-version-href]\n[![bundle][bundle-src]][bundle-href]\n\nThis is an official plugin for [Regex+](https://github.com/slevithan/regex) that adds support for recursive matching up to a specified max depth *N*, where *N* can be between 2 and 100. Generated regexes are native JavaScript `RegExp` instances.\n\nRecursive matching is added to a regex via one of the following (the recursion depth limit is provided in place of *`N`*):\n\n- `(?R=N)` — Recursively match the entire regex at this position.\n- `\\g<name&R=N>` or `\\g<number&R=N>` — Recursively match the contents of the group referenced by name or number at this position.\n  - The `\\g` subroutine must be *within* the referenced group.\n\nMultiple 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. `groups.name` on a match object is the value captured by group `name` at the top level of the recursion stack.\n\n## Install and use\n\n```sh\nnpm install regex regex-recursion\n```\n\n```js\nimport {regex} from 'regex';\nimport {recursion} from 'regex-recursion';\n\nconst re = regex({plugins: [recursion]})`…`;\n```\n\n<details>\n  <summary>Using a global name (no import)</summary>\n\n```html\n<script src=\"https://cdn.jsdelivr.net/npm/regex@5.0.2/dist/regex.min.js\"></script>\n<script src=\"https://cdn.jsdelivr.net/npm/regex-recursion@4.3.0/dist/regex-recursion.min.js\"></script>\n<script>\n  const {regex} = Regex;\n  const {recursion} = Regex.plugins;\n\n  const re = regex({plugins: [recursion]})`…`;\n</script>\n```\n</details>\n\n## Examples\n\n### Match an equal number of two different subpatterns\n\n#### Anywhere within a string\n\n```js\n// Matches sequences of up to 50 'a' chars followed by the same number of 'b'\nconst re = regex({plugins: [recursion]})`a(?R=50)?b`;\nre.exec('test aaaaaabbb')[0];\n// → 'aaabbb'\n```\n\n#### As the entire string\n\n```js\nconst re = regex({plugins: [recursion]})`^\n  (?<balanced>\n    a\n    # Recursively match just the specified group\n    \\g<balanced&R=50>?\n    b\n  )\n$`;\nre.test('aaabbb'); // → true\nre.test('aaabb'); // → false\n```\n\nNotice the `^` and `$` anchors outside of the recursive subpattern.\n\n### Match balanced parentheses\n\n```js\n// Matches all balanced parentheses up to depth 50\nconst parens = regex({flags: 'g', plugins: [recursion]})`\n  \\( ( [^\\(\\)] | (?R=50) )* \\)\n`;\n\n'test ) (balanced ((parens))) () ((a)) ( (b)'.match(parens);\n/* → [\n  '(balanced ((parens)))',\n  '()',\n  '((a))',\n  '(b)'\n] */\n```\n\nFollowing 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](https://www.regular-expressions.info/catastrophic.html).\n\n```js\nconst parens = regex({flags: 'g', plugins: [recursion]})`\n  \\( ( (?> [^\\(\\)]+ ) | (?R=50) )* \\)\n`;\n```\n\nThis 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.\n\n[Atomic groups](https://github.com/slevithan/regex#atomic-groups) are provided by the base `regex` library.\n\n### Match palindromes\n\n#### Match palindromes anywhere within a string\n\n```js\nconst palindromes = regex({flags: 'gi', plugins: [recursion]})`\n  (?<char> \\w )\n  # Recurse, or match a lone unbalanced char in the middle\n  ( (?R=15) | \\w? )\n  \\k<char>\n`;\n\n'Racecar, ABBA, and redivided'.match(palindromes);\n// → ['Racecar', 'ABBA', 'edivide']\n```\n\nIn 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.\n\n#### Match palindromes as complete words\n\n```js\nconst palindromeWords = regex({flags: 'gi', plugins: [recursion]})`\\b\n  (?<palindrome>\n    (?<char> \\w )\n    ( \\g<palindrome&R=15> | \\w? )\n    \\k<char>\n  )\n\\b`;\n\n'Racecar, ABBA, and redivided'.match(palindromeWords);\n// → ['Racecar', 'ABBA']\n```\n\nNotice the `\\b` word boundaries outside of the recursive subpattern.\n\n<!-- Badges -->\n\n[npm-version-src]: https://img.shields.io/npm/v/regex-recursion?color=78C372\n[npm-version-href]: https://npmjs.com/package/regex-recursion\n[bundle-src]: https://img.shields.io/bundlejs/size/regex-recursion?color=78C372&label=minzip\n[bundle-href]: https://bundlejs.com/?q=regex-recursion&treeshake=[*]\n","publish_time":1732710829644,"maintainers":[{"name":"slevithan","email":"steves_list@hotmail.com"}],"_cnpm_publish_time":1732710829644,"dist-tags":{"latest":"6.0.2"}}