Regular Expression String Parser

raw JSON →
2.3.1 verified Thu Apr 23 auth: no javascript maintenance

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.

error TypeError: RegexParser is not a function
cause 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';.
error ReferenceError: require is not defined in ES module scope
cause 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';.
gotcha The 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.
gotcha While 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`.
gotcha Since 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.
npm install regex-parser
yarn add regex-parser
pnpm add regex-parser

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')}`);