{"id":14805,"library":"perf-regexes","title":"Optimized Regular Expressions for JavaScript","description":"The `perf-regexes` package (current version 1.0.1, last updated in late 2018) provides a collection of pre-optimized regular expressions tailored for common parsing tasks in JavaScript. This includes patterns for identifying HTML comments, JavaScript comments (single and multi-line), various types of strings (single and double-quoted), and managing line endings. It offers utilities for detecting empty lines, non-empty lines, trailing whitespace, and normalizing line-ending styles. The library supports both CommonJS and UMD builds, making it usable in Node.js environments (with a minimum requirement of Node.js 6.14) and directly in browsers via a global `R` object. A key differentiator is its focus on robust, pre-built, and tested regex patterns that simplify complex parsing challenges, especially for nested structures or escaped characters, which are notoriously difficult to handle with custom regexes. The package also ships with TypeScript definitions, enhancing developer experience in type-checked environments. Despite its utility, the package has not received updates since 2018, indicating it is no longer actively maintained.","status":"abandoned","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/aMarCruz/perf-regexes","tags":["javascript","html","typescript","comments","regexp","regexes","search","match"],"install":[{"cmd":"npm install perf-regexes","lang":"bash","label":"npm"},{"cmd":"yarn add perf-regexes","lang":"bash","label":"yarn"},{"cmd":"pnpm add perf-regexes","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"`R` is the default export containing all regex patterns as properties. For CommonJS, use `const R = require('perf-regexes');`. In browsers, it's available as `window.R` if loaded via UMD.","wrong":"import { R } from 'perf-regexes';","symbol":"R","correct":"import R from 'perf-regexes';"},{"note":"Individual regexes like `HTML_CMNT` are properties of the default `R` object, not named exports.","wrong":"import { HTML_CMNT } from 'perf-regexes';","symbol":"HTML_CMNT","correct":"import R from 'perf-regexes';\nconst htmlCommentRegex = R.HTML_CMNT;"},{"note":"Regexes are accessed as properties of the default `R` object. This pattern combines `JS_SQSTR` and `JS_DQSTR` for convenience.","wrong":"import { JS_STRING } from 'perf-regexes';","symbol":"JS_STRING","correct":"import R from 'perf-regexes';\nconst jsStringRegex = R.JS_STRING;"},{"note":"This regex is deprecated as of v1.0 and is slated for removal in future versions due to inherent risks in matching literal regexes. Prefer `JS_REGEX` with additional validation.","wrong":"import { JS_REGEX_P } from 'perf-regexes';","symbol":"JS_REGEX_P","correct":"import R from 'perf-regexes';\nconst deprecatedRegex = R.JS_REGEX_P;"}],"quickstart":{"code":"const R = require('perf-regexes');\n\n// Function to remove trailing whitespace, empty lines, and normalize line-endings\nconst cleaner = (text) => text.split(R.OPT_WS_EOL).filter(Boolean).join('\\n');\n\nconsole.log('Cleaned text example:');\nconsole.dir(cleaner(' \\r\\r\\n\\nAA\\t\\t\\t\\r\\n\\rBB\\nCC  \\rDD  '));\n// Expected output: 'AA\\nBB\\nCC\\nDD'\n\n// Use the cleaner function to cleanup HTML text by first removing HTML comments\nconst htmlCleaner = (html) => cleaner(html.replace(R.HTML_CMNT, ''));\n\nconst rawHtml = '\\r<!--header--><h1>A</h1>\\r<div>B<br>\\r\\nC</div> <!--end-->\\n';\nconsole.log('\\nCleaned HTML example:');\nconsole.dir(htmlCleaner(rawHtml));\n// Expected output: '<h1>A</h1>\\n<div>B<br>\\nC</div>'\n\n// Demonstrating string conversion: Double-quoted to single-quoted strings\nconst toSingleQuotes = (text) => text.replace(R.JS_STRING, (str) => {\n  return str[0] === '\"'\n    ? `'${str.slice(1, -1).replace(/'/g, \"\\'\")}'`\n    : str;\n});\n\nconst stringWithQuotes = `\"A's\" 'B' \"C\" \"D\\\\\"E\" 'F\\\\\\'G'`;\nconsole.log('\\nString quote conversion example:');\nconsole.log(toSingleQuotes(stringWithQuotes));\n// Expected output: 'A\\'s' 'B' 'C' 'D\\\"E' 'F\\'G'","lang":"javascript","description":"This quickstart demonstrates how to use `perf-regexes` to clean text by removing empty lines and trailing whitespace, normalize HTML by stripping comments, and convert double-quoted JavaScript strings to single-quoted strings."},"warnings":[{"fix":"Avoid using `JS_REGEX_P`. If you need to identify regexes, use `JS_REGEX` and perform additional validation or consider dedicated parsing libraries.","message":"`JS_REGEX_P` is deprecated as of v1.0 and will be removed in a future minor version. It is highly risky to match literal regexes with other regexes, especially in ES6+ environments.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Before each `regex.exec(text)` call, ensure `regex.lastIndex = 0;` or create a new regex instance, e.g., `const newRegex = new RegExp(R.YOUR_REGEX.source, R.YOUR_REGEX.flags);`.","message":"When using any regex with the global (`'g'`) flag, you must manually reset `lastIndex` before each new `exec` call or clone the regex instance to prevent unexpected behavior and incorrect matches.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure your Node.js environment is version 6.14 or higher. Update Node.js if necessary.","message":"The minimum supported version of NodeJS is now 6.14. Running on older versions may lead to compatibility issues or errors.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Limit the use of `JS_REGEX` to complement other utilities or for less critical parsing tasks. For robust JavaScript parsing, consider a dedicated AST parser or a more sophisticated tokenization library.","message":"The `JS_REGEX` pattern should be used with caution and its results validated. Matching complex JavaScript regexes reliably with simple regex patterns is inherently difficult and prone to edge cases.","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":"Replace `R.JS_REGEX_P` with `R.JS_REGEX` and implement additional validation, or consider using alternative parsing strategies.","cause":"Attempting to use the deprecated `JS_REGEX_P` regex, which has been removed or is no longer accessible.","error":"TypeError: R.JS_REGEX_P is not a function (or similar 'undefined' error)"},{"fix":"Set `yourRegex.lastIndex = 0;` before each new `exec()` call on the same regex instance, or create a new `RegExp` instance each time.","cause":"The `lastIndex` property of a global regex (`g` flag) was not reset between calls to `exec()`, causing it to resume from the previous match's end.","error":"Unexpected empty matches or incorrect parsing when repeatedly calling 'exec()' on a global regex."},{"fix":"In CommonJS, use `const R = require('perf-regexes');`. For ESM, use `import R from 'perf-regexes';`. In a browser, ensure `<script src=\"https://unpkg.com/perf-regexes/index.min.js\"></script>` is loaded before attempting to access `window.R`.","cause":"The `perf-regexes` library's default export `R` was not correctly imported or required in a module environment, or the UMD bundle was not loaded in the browser.","error":"ReferenceError: R is not defined"}],"ecosystem":"npm"}