Optimized Regular Expressions for JavaScript

1.0.1 · abandoned · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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'

view raw JSON →