Regular Expression String Parser
raw JSON →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.
Common errors
error TypeError: RegexParser is not a function ↓
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 ↓
import RegexParser from 'regex-parser';. Warnings
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. ↓
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';`. ↓
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. ↓
Install
npm install regex-parser yarn add regex-parser pnpm add regex-parser Imports
- RegexParser wrong
import { RegexParser } from 'regex-parser';correctimport RegexParser from 'regex-parser'; - RegexParser wrong
import RegexParser from 'regex-parser';correctconst RegexParser = require('regex-parser'); - RegexParser
import type RegexParser from 'regex-parser';
Quickstart
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')}`);