regex-fun is a utility library for JavaScript and TypeScript that facilitates the programmatic construction of regular expressions using a functional, composable API. It provides a comprehensive set of functions like `combine`, `either`, `capture`, and various quantifiers (e.g., `optional`, `anyNumber`, `oneOrMore`, `exactly`, `atLeast`, `between`, and their non-greedy counterparts) to build complex regex patterns from smaller, readable components. A key differentiator is its automatic escaping of string inputs, which prevents common regex syntax errors when embedding literal strings, treating them as fixed text rather than regex patterns. The library ships with TypeScript types, ensuring strong type-checking and autocompletion for users in modern development environments. The current stable version is 3.1.0, and new functions are added on an ad-hoc basis driven by maintainer needs, rather than a strict release cadence.
npm install regex-funVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates building a complex regular expression using `either`, `optional`, `combine`, and `capture` to parse a greeting followed by a word. It also shows the automatic string escaping feature.
To include a raw regex pattern, always wrap it in a `RegExp` literal or constructor: `combine(/your_pattern_here/)` instead of `combine('your_pattern_here')`.Wrap the desired part of your regex in `capture(...)` to create a capturing group, e.g., `combine('prefix', capture(/\w+/), 'suffix')`.Ensure your project is configured for ES Modules by adding `"type": "module"` to your `package.json` file, or by renaming your file to end with `.mjs`. If you must use CommonJS, consider dynamic `import()` or transpilation.
Verify your import statement: `import { combine } from 'regex-fun'`. Check for typos in function names or the package name. Ensure your module resolver is correctly configured if you are using a bundler.No dependency data recorded yet.