{"id":11923,"library":"regenerate","title":"Regenerate: Unicode-Aware Regex Generator","description":"Regenerate is a specialized JavaScript library designed to create regular expressions from a given set of Unicode symbols or code points. It intelligently handles the complexities of Unicode in JavaScript, particularly with astral symbols (those outside the Basic Multilingual Plane), by generating ES5-compatible patterns that correctly match these characters, typically using surrogate pairs. The library provides a fluent, chainable API to add, remove, and manage code points and ranges, allowing developers to precisely define character sets for their regexes. Currently at version 1.4.2, the package appears to be in a maintenance or stable state, having seen its last significant update several years ago, indicating a mature and feature-complete solution for Unicode-aware regex generation. It remains a valuable tool for ensuring cross-browser and historical JavaScript engine compatibility when dealing with advanced Unicode characters in regular expressions.","status":"maintenance","version":"1.4.2","language":"javascript","source_language":"en","source_url":"https://github.com/mathiasbynens/regenerate","tags":["javascript","regex","regexp","unicode","generator","tool"],"install":[{"cmd":"npm install regenerate","lang":"bash","label":"npm"},{"cmd":"yarn add regenerate","lang":"bash","label":"yarn"},{"cmd":"pnpm add regenerate","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is primarily CommonJS (CJS). Direct ESM `import` syntax will typically fail in pure ESM environments without specific Node.js or bundler configuration for CJS interop or if 'type': 'module' is set in package.json.","wrong":"import regenerate from 'regenerate';","symbol":"regenerate","correct":"const regenerate = require('regenerate');"},{"note":"When consumed in an ESM context (e.g., in a TypeScript project or a Node.js module with 'type': 'module'), the CJS default export is often accessible via a namespace import like `import * as regenerate from 'regenerate';`. Avoid named imports for the top-level function.","wrong":"import { regenerate } from 'regenerate';","symbol":"regenerate","correct":"import * as regenerate from 'regenerate';"},{"note":"The `add`, `remove`, `addRange`, `removeRange` methods are part of the chainable API returned by calling the `regenerate` function, not static methods on the `regenerate` export itself.","wrong":"regenerate.add(0x1D306);","symbol":"regenerate().add","correct":"regenerate().add(0x1D306);"}],"quickstart":{"code":"const regenerate = require('regenerate');\n\n// Create a set and add/remove code points and ranges\nconst unicodeSet = regenerate()\n  .addRange(0x60, 0x69) // Add U+0060 (`) to U+0069 (i)\n  .remove(0x62, 0x64) // Remove U+0062 (b) and U+0064 (d)\n  .add(0x1D306); // Add U+1D306 (a rare astral symbol)\n\n// Get the array of code points\nconsole.log('Code points:', unicodeSet.valueOf());\n// Expected: [96, 97, 99, 101, 102, 103, 104, 105, 119558]\n\n// Get the ES5-compatible regex string\nconst regexString = unicodeSet.toString();\nconsole.log('Regex string:', regexString);\n// Expected: '[`ace-i]|\\uD834\\uDF06'\n\n// Get the RegExp object\nconst regex = unicodeSet.toRegExp();\nconsole.log('RegExp object:', regex);\n// Expected: /[`ace-i]|\\uD834\\uDF06/\n\n// Example with direct arguments to regenerate\nconst directSet = regenerate(0x1D306, 'A', '©', 0x2603);\nconsole.log('Direct set regex string:', directSet.toString());\n// Expected: '[A\\xA9\\u2603]|\\uD834\\uDF06'\n","lang":"javascript","description":"This quickstart demonstrates how to initialize a `regenerate` set, add and remove individual code points and ranges, and finally generate both the regex string and the `RegExp` object. It also shows adding multiple values directly during initialization, including astral symbols."},"warnings":[{"fix":"Thoroughly test existing regex generation logic when upgrading from versions older than 0.6.0. Review the generated regex patterns and their behavior, especially with complex Unicode sets.","message":"The v0.6.0 release (from 2013) involved a complete internal rewrite for performance and memory efficiency, based on a new data structure. While the public API aimed to remain compatible, such a significant overhaul could have introduced subtle behavioral changes or regressions for certain edge cases compared to prior 0.x versions.","severity":"breaking","affected_versions":">=0.6.0"},{"fix":"In Node.js ESM, use `import * as regenerate from 'regenerate';`. For TypeScript, ensure `esModuleInterop` is enabled in `tsconfig.json`. When using bundlers, verify their CJS-to-ESM interop settings. Alternatively, stick to `const regenerate = require('regenerate');` in CJS files.","message":"Regenerate is an older package primarily written for CommonJS (CJS) environments. Attempting to use modern ECMAScript Module (ESM) `import` syntax (`import regenerate from 'regenerate';`) directly in a pure ESM Node.js project or a bundler that strictly enforces ESM can lead to import errors or `TypeError: regenerate_1.default is not a function` at runtime.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Evaluate if the lack of ongoing maintenance poses a risk for your project's specific requirements. For most Unicode regex generation needs, the existing functionality should remain robust.","message":"The package is no longer actively maintained. While stable and functional for its intended purpose, it will not receive updates for new JavaScript features, performance improvements from newer JS engines, or bug fixes for newly discovered edge cases or security vulnerabilities (though less critical for this type of utility).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Do not add the `u` flag to regular expressions generated by `regenerate` unless you fully understand its implications and have thoroughly tested the resulting patterns. `regenerate`'s output is designed to work correctly without the `u` flag in older environments, and with it, in modern ones, the surrogate pair handling might conflict with native Unicode matching.","message":"Regenerate specifically generates ES5-compatible regexes that correctly handle astral symbols by converting them to surrogate pairs (e.g., `\\uD834\\uDF06`). If you manually add the `u` flag (Unicode flag) to a regex generated by Regenerate, it might lead to unexpected matching behavior because the `u` flag changes how JavaScript's regex engine interprets character classes and escape sequences.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure you are importing the package correctly. For CommonJS, use `const regenerate = require('regenerate');`. For ESM (especially in TypeScript), use `import * as regenerate from 'regenerate';` to capture the CJS default export as a namespace object, then call `regenerate.default()` or `regenerate()` if your transpiler unwraps it.","cause":"Attempting to call `regenerate` directly after a named ESM import (e.g., `import { regenerate } from 'regenerate';`) or after a misconfigured CJS `require` call in some environments.","error":"TypeError: regenerate is not a function"},{"fix":"Always call the `regenerate()` function first to create a new set instance, then chain its methods: `regenerate().add(0x60).addRange(0x6A, 0x6B);`","cause":"This error occurs when trying to call a method like `add()` directly on the `regenerate` export (e.g., `regenerate.add(...)`) instead of on an instance returned by calling `regenerate()` (e.g., `regenerate().add(...)`).","error":"TypeError: Cannot read properties of undefined (reading 'add')"},{"fix":"Verify your project's `package.json` for `\"type\": \"module\"` and adjust import/export strategies. Ensure your bundler or Node.js environment is correctly configured for CJS-ESM interop. If possible, consider changing the consuming file to use ESM `import` syntax or ensure `esModuleInterop` is enabled in TypeScript.","cause":"This error typically indicates that `regenerate` has been inadvertently treated or bundled as an ESM-only package by a tool or environment, and a CJS `require()` call is attempting to load it. While `regenerate` is CJS, this can happen with build configurations.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module [path/to/node_modules/regenerate/index.js] from [your_file.js] not supported."}],"ecosystem":"npm"}