{"name":"regex-recursion","version":"1.0.0","keywords":["recursion","regex","regexp"],"author":{"name":"Steven Levithan"},"license":"MIT","_id":"regex-recursion@1.0.0","homepage":"https://github.com/slevithan/regex-recursion#readme","bugs":{"url":"https://github.com/slevithan/regex-recursion/issues"},"dist":{"shasum":"c9a8dd054fad6d5b35439168b157e894ddf6358b","tarball":"http://registry-npm.ywwl.com/regex-recursion/download/regex-recursion-1.0.0.tgz","fileCount":8,"integrity":"sha512-04UVa2ajhBu355ozAsCe3FG/ji1MNloq3JG0Y77meLARE5Pc0SHuazNVp9pmJuE9aaxhvx0Hxa+3u1J5NwfBqA==","signatures":[{"sig":"MEUCIQDXK5V5OFkVS5IF4wGflUshr61b+PNFSiay1uuM6IoxsgIgcnlFc4NxdtV8yIW3HFRKSeSuM5+7tiJzxAnE2U+nBk0=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":84956,"size":25313,"noattachment":false,"key":"/regex-recursion/-/regex-recursion-1.0.0.tgz"},"type":"module","exports":"./src/index.js","gitHead":"2b44dc60147bf6e93b7703f067689f95fe11fb03","scripts":{"test":"jasmine","build":"esbuild src/index.js --bundle --minify --sourcemap --outfile=dist/regex-recursion.min.js --global-name=Regex.ext","pretest":"npm run build","prepublish":"npm test"},"_npmUser":{"name":"slevithan","email":"steves_list@hotmail.com"},"repository":{"url":"git+https://github.com/slevithan/regex-recursion.git","type":"git"},"_npmVersion":"10.7.0","description":"Recursive matching extension for tag regex","directories":{},"_nodeVersion":"22.2.0","dependencies":{"regex":"^1.1.1","regex-utilities":"^1.0.0"},"_hasShrinkwrap":false,"devDependencies":{"esbuild":"^0.21.4","jasmine":"^5.1.0"},"_npmOperationalInternal":{"tmp":"tmp/regex-recursion_1.0.0_1718060667548_0.2426371872816342","host":"s3://npm-registry-packages"},"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":1718060667756,"maintainers":[{"name":"slevithan","email":"steves_list@hotmail.com"}],"_cnpm_publish_time":1718060667756,"dist-tags":{"latest":"6.0.2"}}