{"id":17919,"library":"regex-parser","title":"Regular Expression String Parser","description":"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.","status":"maintenance","version":"2.3.1","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/IonicaBizau/regex-parser.js","tags":["javascript","regular","expressions","node","parser","string","typescript"],"install":[{"cmd":"npm install regex-parser","lang":"bash","label":"npm"},{"cmd":"yarn add regex-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add regex-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The default export `RegexParser` should be used for ESM imports, not a named import.","wrong":"import { RegexParser } from 'regex-parser';","symbol":"RegexParser","correct":"import RegexParser from 'regex-parser';"},{"note":"For CommonJS environments, `require` is the correct way to import the module.","wrong":"import RegexParser from 'regex-parser';","symbol":"RegexParser","correct":"const RegexParser = require('regex-parser');"},{"note":"When using TypeScript, you can import the type definition to ensure correct type checking, although it's typically inferred.","symbol":"RegexParser","correct":"import type RegexParser from 'regex-parser';"}],"quickstart":{"code":"import RegexParser from 'regex-parser';\n\n// Example 1: Basic parsing\nconst regexString1 = \"/^hello world$/i\";\nconst parsedRegex1 = RegexParser(regexString1);\nconsole.log(`Parsed regex 1: ${parsedRegex1}`);\nconsole.log(`Type: ${parsedRegex1.constructor.name}`);\nconsole.log(`Flags: ${parsedRegex1.flags}`);\n\n// Example 2: Regex with global flag\nconst regexString2 = \"/search/g\";\nconst parsedRegex2 = RegexParser(regexString2);\nconsole.log(`Parsed regex 2: ${parsedRegex2}`);\nconsole.log(`Test 'searchable': ${parsedRegex2.test('searchable')}`);\n\n// Example 3: Invalid flags (graceful handling since v2.3.0)\nconst regexString3 = \"/pattern/xyz\";\nconst parsedRegex3 = RegexParser(regexString3);\nconsole.log(`Parsed regex 3 with invalid flags: ${parsedRegex3}`);\n// Note: Invalid flags will typically be ignored, resulting in a RegExp without those flags.\n\n// Example 4: Parsing a string that is not a regex literal\n// The parser expects a regex literal string, e.g., '/pattern/flags'.\n// If a plain string is provided, it might be interpreted as a pattern without flags.\nconst plainString = \"just some text\";\nconst parsedPlain = RegexParser(plainString);\nconsole.log(`Parsed plain string: ${parsedPlain}`);\nconsole.log(`Test 'just some text': ${parsedPlain.test('just some text')}`);","lang":"typescript","description":"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."},"warnings":[{"fix":"Ensure the input string is correctly formatted as a regex literal: `RegexParser(\"/my_pattern/i\")` if you want flags to be parsed.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For ESM/TypeScript, always use `import RegexParser from 'regex-parser';`. If you encounter type issues, ensure your `tsconfig.json` has `\"esModuleInterop\": true`.","message":"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';`.","severity":"gotcha","affected_versions":">=2.2.9"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=2.3.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Change `const RegexParser = require('regex-parser');` or `import { RegexParser } from 'regex-parser';` to `import RegexParser from 'regex-parser';`.","cause":"Incorrect import statement in an ESM or TypeScript environment attempting to use CommonJS `require` or a named import instead of the default export.","error":"TypeError: RegexParser is not a function"},{"fix":"Switch to ESM import syntax: `import RegexParser from 'regex-parser';`.","cause":"Attempting to use `require()` syntax within an ECMAScript module (ESM) environment, which does not support CommonJS `require`.","error":"ReferenceError: require is not defined in ES module scope"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}