JSPM

escape-regex-string

1.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5677
  • Score
    100M100P100Q126968F
  • License MIT

An extremely simple module to escape a string literal in preparation for passing to the RegExp constructor.

Package Exports

  • escape-regex-string

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 (escape-regex-string) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

escape-regex-string

An extremely simple Node module to escape a string literal in preparation for passing to the RegExp constructor.

Interface

escape-regex-string

(require('escape-regex-string'))(patternString[, escapeCharsRegex])
  • patternString String
  • escapeCharsRegex Regexp
    • Defaults to value of member escape-regex-string.defaultEscapeCharsRegex (see below)

Returns the passed patternString with all RegExp tokens escaped.

escape-regex-string.defaultEscapeCharsRegex

(require('escape-regex-string')).defaultEscapeCharsRegex

A read-only RegExp instance containing the default pattern used to escape passed strings. If a RegExp instance is manually passed into a function call to this module, the passed RegExp value will be used instead of this default value.

Example Usage

var escapeRegexString = require('escape-regex-string');
var regexString = '$&*()awsd';
var escapedRegexString = escapeRegexString(regexString); // '\\$&\\*\\(\\)awsd'
var regExpObject = new RegExp(escapedRegexString);
console.log(regExpObject); // /\$&\*\(\)awsd/

Feedback

I wrote this miniature module to practice with a few of the tools, libraries, and workflows available to JS developers. I welcome constructive criticism and advice.