Registry / serialization / ts-regex-builder

ts-regex-builder

JSON →
library1.8.2jsnpmunverified

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.

npm install ts-regex-builder
INSTALL
IMPORT
SIG · TS-REGEX-BUILDER
T
ts-regex-builder
serializationjavascriptv1.8.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

buildRegExp
import { buildRegExp } from 'ts-regex-builder'
import buildRegExp from 'ts-regex-builder'
Named export; default import is not available. ESM-only since v1.5.0.
capture
import { capture } from 'ts-regex-builder'
const { capture } = require('ts-regex-builder')
ESM-only; CommonJS require() will fail in Node >= 18.
choiceOf
import { choiceOf } from 'ts-regex-builder'
import { choiceOf } from 'ts-regex-builder'
Correct usage shown. No common wrong pattern beyond CJS require.

Shows regex building with constructs like capture, choiceOf, repeat, and charRange, plus usage of test() and exec().

import { buildRegExp, capture, oneOrMore, digit, startOfString, endOfString, optional, choiceOf, charRange, repeat, anyOf, word } from 'ts-regex-builder'; // Match a hex color (#rgb or #rrggbb) const hexDigit = /[a-fA-F0-9]/; const hexColor = buildRegExp([ startOfString, optional('#'), capture( choiceOf( repeat(hexDigit, 6), // #rrggbb repeat(hexDigit, 3), // #rgb ), ), endOfString, ]); console.log(hexColor.test('#abc')); // true console.log(hexColor.test('#123456')); // true console.log(hexColor.test('xyz')); // false // Match currency amounts: $100, €50, USD 25.50 const currencyCode = repeat(charRange('A', 'Z'), 3); const currencyAmount = buildRegExp([ choiceOf('$', '€', currencyCode), capture( oneOrMore(digit), optional(['.', repeat(digit, 2)]), ), ]); console.log(currencyAmount.test('$100')); // true console.log(currencyAmount.test('USD 25.50')); // true console.log(currencyAmount.exec('€50')); // ['€50', '50']
Debug
Known issues
breakingESM-only since v1.5.0; CommonJS require() will throw.
fix
Use import syntax or switch to dynamic import().
affects: >=1.5.0
deprecatedThe 'any' construct is deprecated since v1.4.0; use '.' (string) instead.
fix
Replace any() with '.' in regex sequences.
affects: >=1.4.0
gotchaRegExp literals passed directly are not escaped; they must be valid regex patterns.
fix
Ensure strings used in sequences are escaped if needed, or use construct methods.
affects: >=1.0.0
gotchaNode version requirement >= 18.0.0; may fail on older runtimes.
fix
Use Node 18+ or configure bundler to polyfill.
affects: >=1.0.0
deprecatedThe 'wordBoundary' construct was renamed to 'wordBoundary' (already correct); no action needed.
fix
No fix needed; name is stable.
affects: >=1.3.0
Errors
Common errors & fixes
Cannot find module 'ts-regex-builder' or its corresponding type declarations.
Project uses CommonJS and Node < 18 or missing ESM support.
fix
Upgrade to Node >= 18 and use ESM (type: 'module' in package.json or .mjs extension).
TypeError: ts_regex_builder_1.buildRegExp is not a function
Default import used instead of named import.
fix
Change to import { buildRegExp } from 'ts-regex-builder'
SyntaxError: Unexpected token '?' (at ...)
Using optional chaining on regex construct results incorrectly.
fix
Ensure regex constructs are used within buildRegExp arrays.
Upgrade
Version history
1.8.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
OpenAI (training)
1
Resources
ts-regex-builder — npm install ts-regex-builder · libregistry