Registry / serialization / regex-parser

regex-parser

JSON →
library2.3.1jsnpmunverified

regex-parser is a JavaScript utility module designed to parse a string representation of a regular expression and return a `RegExp` object. Its current stable version is 2.3.1. The package has seen infrequent updates, primarily focusing on documentation, license year, and the addition of TypeScript typings (since 2.2.9). It's a focused tool for environments where regular expressions are provided as strings and need to be converted to native `RegExp` objects, handling both the pattern and flags. Key differentiators include its simplicity and explicit support for parsing invalid flags gracefully (since 2.3.0). It doesn't offer complex regex manipulation or validation beyond standard JavaScript `RegExp` behavior, making it a lightweight option for this specific parsing task.

npm install regex-parser
INSTALL
IMPORT
SIG · REGEX-PARSER
R
regex-parser
serializationjavascriptv2.3.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

RegexParser
import RegexParser from 'regex-parser';
import { RegexParser } from 'regex-parser';
The default export `RegexParser` should be used for ESM imports, not a named import.
RegexParser
const RegexParser = require('regex-parser');
import RegexParser from 'regex-parser';
For CommonJS environments, `require` is the correct way to import the module.
RegexParser
import type RegexParser from 'regex-parser';
When using TypeScript, you can import the type definition to ensure correct type checking, although it's typically inferred.

This quickstart demonstrates how to parse various string representations of regular expressions, including those with flags, and illustrates the handling of invalid flags and non-literal strings.

import RegexParser from 'regex-parser'; // Example 1: Basic parsing const regexString1 = "/^hello world$/i"; const parsedRegex1 = RegexParser(regexString1); console.log(`Parsed regex 1: ${parsedRegex1}`); console.log(`Type: ${parsedRegex1.constructor.name}`); console.log(`Flags: ${parsedRegex1.flags}`); // Example 2: Regex with global flag const regexString2 = "/search/g"; const parsedRegex2 = RegexParser(regexString2); console.log(`Parsed regex 2: ${parsedRegex2}`); console.log(`Test 'searchable': ${parsedRegex2.test('searchable')}`); // Example 3: Invalid flags (graceful handling since v2.3.0) const regexString3 = "/pattern/xyz"; const parsedRegex3 = RegexParser(regexString3); console.log(`Parsed regex 3 with invalid flags: ${parsedRegex3}`); // Note: Invalid flags will typically be ignored, resulting in a RegExp without those flags. // Example 4: Parsing a string that is not a regex literal // The parser expects a regex literal string, e.g., '/pattern/flags'. // If a plain string is provided, it might be interpreted as a pattern without flags. const plainString = "just some text"; const parsedPlain = RegexParser(plainString); console.log(`Parsed plain string: ${parsedPlain}`); console.log(`Test 'just some text': ${parsedPlain.test('just some text')}`);
Debug
Known issues
gotchaThe parser expects input strings to be in the format of a regular expression literal (e.g., `/pattern/flags`). Providing a plain string like `"some text"` will result in `/some text/` without specific flags unless explicitly added. This is by design but can be unexpected if you intend to parse only the pattern part.
fix
Ensure the input string is correctly formatted as a regex literal: `RegexParser("/my_pattern/i")` if you want flags to be parsed.
affects: >=1.0.0
gotchaWhile the package ships TypeScript types and supports ESM, the primary examples in older documentation and npm still use CommonJS `require()`. In modern TypeScript or pure ESM projects, use `import RegexParser from 'regex-parser';`.
fix
For ESM/TypeScript, always use `import RegexParser from 'regex-parser';`. If you encounter type issues, ensure your `tsconfig.json` has `"esModuleInterop": true`.
affects: >=2.2.9
gotchaSince version 2.3.0, the package gracefully handles invalid flags by ignoring them. While this prevents errors, it means a regex like `/pattern/xyz` will be parsed as `/pattern/` (or `/pattern/y` if 'y' were a valid flag) without warning that 'x' and 'z' were invalid. This might mask typos in flag declarations.
fix
Always double-check the flags in your input string to ensure they are standard `RegExp` flags (g, i, m, s, u, y, d, v). If strict validation of flags is needed, preprocess the string or check the flags of the returned `RegExp` object.
affects: >=2.3.0
Errors
Common errors & fixes
TypeError: RegexParser is not a function
Incorrect import statement in an ESM or TypeScript environment attempting to use CommonJS `require` or a named import instead of the default export.
fix
Change `const RegexParser = require('regex-parser');` or `import { RegexParser } from 'regex-parser';` to `import RegexParser from 'regex-parser';`.
ReferenceError: require is not defined in ES module scope
Attempting to use `require()` syntax within an ECMAScript module (ESM) environment, which does not support CommonJS `require`.
fix
Switch to ESM import syntax: `import RegexParser from 'regex-parser';`.
Upgrade
Version history
2.3.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
regex-parser — npm install regex-parser · libregistry