Registry / serialization / perf-regexes

perf-regexes

JSON →
library1.0.1jsnpmunverified

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.

npm install perf-regexes
INSTALL
IMPORT
SIG · PERF-REGEXES
P
perf-regexes
serializationjavascriptv1.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

R
import R from 'perf-regexes';
import { R } from 'perf-regexes';
`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.
HTML_CMNT
import R from 'perf-regexes'; const htmlCommentRegex = R.HTML_CMNT;
import { HTML_CMNT } from 'perf-regexes';
Individual regexes like `HTML_CMNT` are properties of the default `R` object, not named exports.
JS_STRING
import R from 'perf-regexes'; const jsStringRegex = R.JS_STRING;
import { JS_STRING } from 'perf-regexes';
Regexes are accessed as properties of the default `R` object. This pattern combines `JS_SQSTR` and `JS_DQSTR` for convenience.
JS_REGEX_P
import R from 'perf-regexes'; const deprecatedRegex = R.JS_REGEX_P;
import { JS_REGEX_P } from 'perf-regexes';
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.

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.

const R = require('perf-regexes'); // Function to remove trailing whitespace, empty lines, and normalize line-endings const cleaner = (text) => text.split(R.OPT_WS_EOL).filter(Boolean).join('\n'); console.log('Cleaned text example:'); console.dir(cleaner(' \r\r\n\nAA\t\t\t\r\n\rBB\nCC \rDD ')); // Expected output: 'AA\nBB\nCC\nDD' // Use the cleaner function to cleanup HTML text by first removing HTML comments const htmlCleaner = (html) => cleaner(html.replace(R.HTML_CMNT, '')); const rawHtml = '\r<!--header--><h1>A</h1>\r<div>B<br>\r\nC</div> <!--end-->\n'; console.log('\nCleaned HTML example:'); console.dir(htmlCleaner(rawHtml)); // Expected output: '<h1>A</h1>\n<div>B<br>\nC</div>' // Demonstrating string conversion: Double-quoted to single-quoted strings const toSingleQuotes = (text) => text.replace(R.JS_STRING, (str) => { return str[0] === '"' ? `'${str.slice(1, -1).replace(/'/g, "\'")}'` : str; }); const stringWithQuotes = `"A's" 'B' "C" "D\\"E" 'F\\\'G'`; console.log('\nString quote conversion example:'); console.log(toSingleQuotes(stringWithQuotes)); // Expected output: 'A\'s' 'B' 'C' 'D\"E' 'F\'G'
Debug
Known issues
deprecated`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.
fix
Avoid using `JS_REGEX_P`. If you need to identify regexes, use `JS_REGEX` and perform additional validation or consider dedicated parsing libraries.
affects: >=1.0.0
gotchaWhen 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.
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);`.
affects: >=1.0.0
breakingThe minimum supported version of NodeJS is now 6.14. Running on older versions may lead to compatibility issues or errors.
fix
Ensure your Node.js environment is version 6.14 or higher. Update Node.js if necessary.
affects: >=1.0.0
gotchaThe `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.
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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: R.JS_REGEX_P is not a function (or similar 'undefined' error)
Attempting to use the deprecated `JS_REGEX_P` regex, which has been removed or is no longer accessible.
fix
Replace `R.JS_REGEX_P` with `R.JS_REGEX` and implement additional validation, or consider using alternative parsing strategies.
Unexpected empty matches or incorrect parsing when repeatedly calling 'exec()' on a global regex.
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.
fix
Set `yourRegex.lastIndex = 0;` before each new `exec()` call on the same regex instance, or create a new `RegExp` instance each time.
ReferenceError: R is not defined
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.
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`.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
perf-regexes — npm install perf-regexes · libregistry