JSPM

perf-regexes

0.1.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 122429
  • Score
    100M100P100Q167437F
  • License MIT

Optimized and powerful regexes for JavaScript

Package Exports

  • perf-regexes

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (perf-regexes) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Build Status npm Version License

perf-regexes

Optimized and powerful regexes for JavaScript

Install

npm install perf-regexes --save

Included Regexes

Name Description Note
JS_MLCMNT Valid multiline JS comment Excludes invalid comments like /*/, supports nested '/*'
JS_SLCMNT Single line JS comment From the // to the (but not including) next EOL
JS_STRING Single and double quoted JS string Handles nested quotes and escaped eols
JS_REGEX Literal regex Can match a divisor, so the match must be validated
JS_REGEX_P Literal regex Captures a prefix in $1, the regex is not captured
HTML_CMNT Valid HTML comments HTML comments with <!-- and --> delimiters

All the regexes has the option 'g' and nothing more, so you can use it with exec or replace.

You don't need the option 'm', perf-regexes works with Win/Mac/Unix EOLs with no problems, but if you like use the RegExp constructor with the source property to recrate the regex like in the example.

NOTE:

Because the 'g', always set lastIndex before using a regex with the exec method.

Example

/*
  Comments removal made easy.
  ...but please don't use this for ES6 template strings
*/
const _R = require('perf-regexes')

function removeComments(source) {
  const re = new RegExp([
    _R.JS_MLCOMM.source,
    _R.JS_SLCOMM.source,
    _R.JS_STRING.source,
    _R.JS_REGEX_P.source].join('|'), 'gm');

  return source.replace(re, match => {
    return /^\/[/\*]/.test(match) ? ' ' : match
  })
}

Matching Regexes

It is not easy. Depending on code complexity, JS_REGEX_P can do the work with 99% accuracy, but you need handle the prefix captured in $1.

Also, this fail matching literal regexes starting with // or />, please follow the best-practices and use /\/ or /\>.

ES6 Template Strings

There's no secure way to match ES6 Template Literals with regexes.