{"id":28420,"library":"ts-regex-builder","title":"ts-regex-builder","description":"A TypeScript-first library for building maintainable regular expressions using a structured, composable DSL. Version 1.8.2 (September 2024) supports Unicode mode, named capture groups, lookahead/lookbehind assertions, and tree-shaking optimizations. Unlike raw regex strings or other builders (e.g., verbal-expressions), ts-regex-builder provides full TypeScript type safety, automatic escaping, and seamless interop with native RegExp. Released under the Callstack organization with monthly updates.","status":"active","version":"1.8.2","language":"javascript","source_language":"en","source_url":"https://github.com/callstack/ts-regex-builder","tags":["javascript","regex","typescript"],"install":[{"cmd":"npm install ts-regex-builder","lang":"bash","label":"npm"},{"cmd":"yarn add ts-regex-builder","lang":"bash","label":"yarn"},{"cmd":"pnpm add ts-regex-builder","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Named export; default import is not available. ESM-only since v1.5.0.","wrong":"import buildRegExp from 'ts-regex-builder'","symbol":"buildRegExp","correct":"import { buildRegExp } from 'ts-regex-builder'"},{"note":"ESM-only; CommonJS require() will fail in Node >= 18.","wrong":"const { capture } = require('ts-regex-builder')","symbol":"capture","correct":"import { capture } from 'ts-regex-builder'"},{"note":"Correct usage shown. No common wrong pattern beyond CJS require.","wrong":"import { choiceOf } from 'ts-regex-builder'","symbol":"choiceOf","correct":"import { choiceOf } from 'ts-regex-builder'"}],"quickstart":{"code":"import { buildRegExp, capture, oneOrMore, digit, startOfString, endOfString, optional, choiceOf, charRange, repeat, anyOf, word } from 'ts-regex-builder';\n\n// Match a hex color (#rgb or #rrggbb)\nconst hexDigit = /[a-fA-F0-9]/;\nconst hexColor = buildRegExp([\n  startOfString,\n  optional('#'),\n  capture(\n    choiceOf(\n      repeat(hexDigit, 6), // #rrggbb\n      repeat(hexDigit, 3), // #rgb\n    ),\n  ),\n  endOfString,\n]);\n\nconsole.log(hexColor.test('#abc')); // true\nconsole.log(hexColor.test('#123456')); // true\nconsole.log(hexColor.test('xyz')); // false\n\n// Match currency amounts: $100, €50, USD 25.50\nconst currencyCode = repeat(charRange('A', 'Z'), 3);\nconst currencyAmount = buildRegExp([\n  choiceOf('$', '€', currencyCode),\n  capture(\n    oneOrMore(digit),\n    optional(['.', repeat(digit, 2)]),\n  ),\n]);\n\nconsole.log(currencyAmount.test('$100')); // true\nconsole.log(currencyAmount.test('USD 25.50')); // true\nconsole.log(currencyAmount.exec('€50')); // ['€50', '50']","lang":"typescript","description":"Shows regex building with constructs like capture, choiceOf, repeat, and charRange, plus usage of test() and exec()."},"warnings":[{"fix":"Use import syntax or switch to dynamic import().","message":"ESM-only since v1.5.0; CommonJS require() will throw.","severity":"breaking","affected_versions":">=1.5.0"},{"fix":"Replace any() with '.' in regex sequences.","message":"The 'any' construct is deprecated since v1.4.0; use '.' (string) instead.","severity":"deprecated","affected_versions":">=1.4.0"},{"fix":"Ensure strings used in sequences are escaped if needed, or use construct methods.","message":"RegExp literals passed directly are not escaped; they must be valid regex patterns.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use Node 18+ or configure bundler to polyfill.","message":"Node version requirement >= 18.0.0; may fail on older runtimes.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"No fix needed; name is stable.","message":"The 'wordBoundary' construct was renamed to 'wordBoundary' (already correct); no action needed.","severity":"deprecated","affected_versions":">=1.3.0"}],"env_vars":null,"last_verified":"2026-05-09T00:00:00.000Z","next_check":"2026-08-07T00:00:00.000Z","problems":[{"fix":"Upgrade to Node >= 18 and use ESM (type: 'module' in package.json or .mjs extension).","cause":"Project uses CommonJS and Node < 18 or missing ESM support.","error":"Cannot find module 'ts-regex-builder' or its corresponding type declarations."},{"fix":"Change to import { buildRegExp } from 'ts-regex-builder'","cause":"Default import used instead of named import.","error":"TypeError: ts_regex_builder_1.buildRegExp is not a function"},{"fix":"Ensure regex constructs are used within buildRegExp arrays.","cause":"Using optional chaining on regex construct results incorrectly.","error":"SyntaxError: Unexpected token '?' (at ...)"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}