{"_id":"escape-regex-string","_rev":"10-ade57958b484774e6f57d0bc339aa13d","name":"escape-regex-string","description":"Escapes a string literal for use as an argument in the standard RegExp constructor.","dist-tags":{"latest":"1.0.6"},"versions":{"1.0.2":{"name":"escape-regex-string","version":"1.0.2","description":"An extremely simple module to escape a string literal in preparation for passing to the RegExp constructor.","main":"index.js","repository":{"type":"git","url":"https://github.com/monotonee/escape-regex-string.git"},"keywords":["regex","RegExp","escape","string"],"author":{"name":"monotonee","email":"monotonee@users.noreply.github.com"},"license":"MIT","bugs":{"url":"https://github.com/monotonee/escape-regex-string/issues"},"devDependencies":{"jscs":"^1.10.0","jsdoc":"^3.3.0-alpha13","jshint":"^2.5.11","mocha":"^2.1.0","npm-check-updates":"^1.5.0"},"gitHead":"fd228d8d9182d35ce93741e7a6ea2598beeea487","homepage":"https://github.com/monotonee/escape-regex-string","_id":"escape-regex-string@1.0.2","scripts":{},"_shasum":"2c39955e9e87bb928662f9ee0278d64182ad30d2","_from":".","_npmVersion":"2.2.0","_nodeVersion":"0.10.29","_npmUser":{"name":"monotonee","email":"monotonee@users.noreply.github.com"},"maintainers":[{"name":"monotonee","email":"monotonee@users.noreply.github.com"}],"dist":{"shasum":"2c39955e9e87bb928662f9ee0278d64182ad30d2","tarball":"https://registry.npmjs.org/escape-regex-string/-/escape-regex-string-1.0.2.tgz","integrity":"sha512-RAZXDgVKAlCEls0DnP9U7EYt2DnOJQP/ClI7DiQfKPQfWGzYOVROpcaWhdEsCloL86SYDwLeSEkhhGff3qKrzw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIG49zwvpoiqYF3IH5Nda2yGKsY00f+FalTJ5GN2+G+d7AiALPcDe+HAhz/ld8/l2bbQyBjThE+yLkekrRiq/SmwiGg=="}]},"directories":{}},"1.0.3":{"name":"escape-regex-string","version":"1.0.3","description":"An extremely simple module to escape a string literal in preparation for passing to the RegExp constructor.","main":"index.js","repository":{"type":"git","url":"https://github.com/monotonee/escape-regex-string.git"},"keywords":["regex","RegExp","escape","string"],"author":{"name":"monotonee","email":"monotonee@users.noreply.github.com"},"license":"MIT","bugs":{"url":"https://github.com/monotonee/escape-regex-string/issues"},"devDependencies":{"jscs":"^1.10.0","jsdoc":"^3.3.0-alpha13","jshint":"^2.6.0","mocha":"^2.1.0","npm-check-updates":"^1.5.0"},"scripts":{"generate-docs":"make docs","test":"make all"},"gitHead":"61bf9bf6386d671c7c0cac77316a07c25bd41d59","homepage":"https://github.com/monotonee/escape-regex-string","_id":"escape-regex-string@1.0.3","_shasum":"56721ff49c66e2ec912b478114a642dd39d607b8","_from":".","_npmVersion":"2.2.0","_nodeVersion":"0.10.29","_npmUser":{"name":"monotonee","email":"monotonee@users.noreply.github.com"},"maintainers":[{"name":"monotonee","email":"monotonee@users.noreply.github.com"}],"dist":{"shasum":"56721ff49c66e2ec912b478114a642dd39d607b8","tarball":"https://registry.npmjs.org/escape-regex-string/-/escape-regex-string-1.0.3.tgz","integrity":"sha512-r5OoxcGgdKyvTDxRhXOAzC41wF/wpXefamZmrDlVdesPrcHtgatF0iV1KPB5D3obeHTXD/0lFjK16jeDBXPb5A==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHWfLxBjC6XjAyyz7x6Eo1ai5LSpH7nd3ILMuaRvM3UxAiEAyQCu5ojY05szVlmlH/hB2+/zqR4AeHtq2GGBz0z4bbk="}]},"directories":{}},"1.0.4":{"name":"escape-regex-string","version":"1.0.4","description":"Escapes a string literal for use as an argument in the standard RegExp constructor.","keywords":["regex","RegExp","escape","string"],"bugs":{"url":"https://github.com/monotonee/escape-regex-string/issues"},"license":"MIT","author":{"name":"monotonee","email":"monotonee@tuta.io"},"main":"index.js","repository":{"type":"git","url":"git+https://github.com/monotonee/escape-regex-string.git"},"devDependencies":{"jscs":"^2.1.0","jsdoc":"^3.3.2","jshint":"^2.8.0","mocha":"^2.2.5"},"scripts":{"test":"make"},"engines":{"iojs":">=1.0.0","node":">=0.10"},"gitHead":"5ad45734658b2657f4df1f9fbfa247c611554e77","homepage":"https://github.com/monotonee/escape-regex-string#readme","_id":"escape-regex-string@1.0.4","_shasum":"3a35d77e209d26b6724bc1e8dcd4120b175b88dd","_from":".","_npmVersion":"2.13.5","_nodeVersion":"0.12.7","_npmUser":{"name":"monotonee","email":"monotonee@tuta.io"},"dist":{"shasum":"3a35d77e209d26b6724bc1e8dcd4120b175b88dd","tarball":"https://registry.npmjs.org/escape-regex-string/-/escape-regex-string-1.0.4.tgz","integrity":"sha512-9wzyWnkmNcNvmrwawiICRCvKNZc6l/OmgmpvKESsqeYJOiOV6tueKHgufYQ8P8ByENpNPNA6F9G9eVB83ij3eQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEWYwnjPCayzbsFR1l2UZCGWzLjGc+6gRLUYwFDsqAq2AiBkBdcXhYHlShb4LuMWlIyulCJAPAuxEL3Bvo1ceG8XuA=="}]},"maintainers":[{"name":"monotonee","email":"monotonee@users.noreply.github.com"}],"directories":{}},"1.0.6":{"name":"escape-regex-string","version":"1.0.6","description":"Escapes a string literal for use as an argument in the standard RegExp constructor.","keywords":["regex","RegExp","escape","string"],"homepage":"https://github.com/monotonee/escape-regex-string/","bugs":{"url":"https://github.com/monotonee/escape-regex-string/issues"},"license":"MIT","author":{"name":"monotonee","email":"monotonee@tuta.io"},"main":"index.js","repository":{"type":"git","url":"git+https://github.com/monotonee/escape-regex-string.git"},"dependencies":{},"devDependencies":{"eslint":"~4","jsdoc":"~3","mocha":"~5"},"scripts":{"test":"make"},"engines":{"node":">=0.10"},"gitHead":"0a721c77d3b06e07928399f42b87f8ab2e541cc2","_id":"escape-regex-string@1.0.6","_npmVersion":"5.6.0","_nodeVersion":"9.5.0","_npmUser":{"name":"monotonee","email":"monotonee@tuta.io"},"dist":{"integrity":"sha512-AcaPwAOKpF52ETT4uu4vmpqQphuQGjIEhdDwG/gAJYah8ixJlrSBFjhtLMT7frc9y3RWgOnLov9E0MsRBdeRbw==","shasum":"f536d8997ab19ec395385dcf2ea28b4e080899eb","tarball":"https://registry.npmjs.org/escape-regex-string/-/escape-regex-string-1.0.6.tgz","fileCount":4,"unpackedSize":9658,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQD+uvNoRe4r0XxHKWi1CCzBlZfCLCpCZ21NPXDKIUL6rwIhAPycuyCjfX86RaY6aThUQGxL+OTJxaxt0qvdJ6r8U67R"}]},"maintainers":[{"name":"monotonee","email":"monotonee@users.noreply.github.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/escape-regex-string_1.0.6_1518129744906_0.8446193293855728"},"_hasShrinkwrap":false}},"readme":"# escape-regex-string [![Build Status](https://travis-ci.org/monotonee/escape-regex-string.svg?branch=master)](https://travis-ci.org/monotonee/escape-regex-string) [![npm version](https://badge.fury.io/js/escape-regex-string.svg)](https://www.npmjs.com/package/escape-regex-string)\n\nEscapes a string literal for use as an argument in the standard RegExp constructor.\n\n## Interface\n\n#### escape-regex-string\n\n```javascript\n(require('escape-regex-string'))(unescapedString[, escapeCharsRegex])\n```\n* unescapedString [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)\n  * The regular expression pattern string in which all special characters need to be escaped.\n* escapeCharsRegex [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)\n  * Defaults to value of member escape-regex-string.defaultEscapeCharsRegex (see below)\n\nReturns the passed unescapedString with all RegExp special characters escaped.\n\n#### escape-regex-string.defaultEscapeCharsRegex\n\n```javascript\n(require('escape-regex-string')).defaultEscapeCharsRegex\n```\nA read-only RegExp instance containing the default pattern used to escape passed strings. If a RegExp instance is\nmanually passed into a function call to this module, the passed RegExp value will be used instead of this default value.\n\n## Example Usage\n```javascript\nlet escapeRegexString = require('escape-regex-string');\nlet regexPatternString = '$&*()awsd';\nlet escapedRegexPatternString = escapeRegexString(regexPatternString);\nconsole.log(escapedRegexPatternString); // '\\\\$&\\\\*\\\\(\\\\)awsd'\nlet regExpObject = new RegExp(escapedRegexPatternString);\nconsole.log(regExpObject); // /\\$&\\*\\(\\)awsd/\n```\n\n## Explanation\nThe built-in JavaScript [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)\nobject's constructor accepts a regular expression pattern in the form of another RegExp instance **or a string.** And,\nas we know, the JavaScript regular expression syntax includes a set of characters with special meaning when interpreted\nby the regex engine, such as the caret (^) and the asterisk (*). A problem is introduced when one attempts to create a\nregular expression pattern in a *string* (*not* a\n[RegExp literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Description)),\nfor use in the RegExp constructor, that contains special regular expression characters *to be matched literally.*\nConsider the following extra-verbose example:\n\nA developer wants to use a RegExp to match caret characters in a given string:\n```javascript\nlet inputStr = 'a^2';\n```\nFor whatever reason, the dev is unwilling or unable to use the RegExp literal syntax and therefore attempts to define a\nregular expression pattern *in a string* to be passed to the RegExp constructor:\n```javascript\nlet firstPattern = '^';\nlet firstRegExp = new RegExp(firstPattern);\n```\nWhen the expression is evaluated, the results are incorrect because the caret is a regular expression special character\nrepresenting the [beginning of the input](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-caret).\nThe beginning of the subject string will therefore be the only match:\n```javascript\nlet firstResult = firstRegExp.exec(inputStr);\nconsole.log(firstResult); // [ '', index: 0, input: 'a^2' ]\n```\nThe developer realizes he needs to escape the special caret character and therefore redefines his pattern with the\nstandard backslash escape character:\n```javascript\nlet secondPattern = '\\^';\nlet secondRegExp = new RegExp(secondPattern);\nlet secondResult = secondRegExp.exec(inputStr);\nconsole.log(secondResult); // [ '', index: 0, input: 'a^2' ]\n```\nWhen the developer evaluates the second expression, however, the results are the same. This is because JavaScript *will\nfirst evaluate the backslash escape character in the pattern string,* before it ever reaches the RegExp constructor.\nWhen the developer eexamines the second pattern string, it is identical to the first despite the added backslash.\n```javascript\nconsole.log(firstPattern === secondPattern); // true\n```\nFinally, the developer adds a second backslash to the pattern string, causing the JavaScript interpreter to preserve a\nsingle backslash character in the string stored in memory. This single backslash is successfully passed to the RegExp\nconstructor, the expression therefore interprets the caret character literally, and the caret is correctly matched\ninside the input string:\n```javascript\nlet finalPattern = '\\\\^';\nlet finalRegExp = new RegExp(finalPattern);\nlet finalResult = finalRegExp.exec(inputStr);\nconsole.log(finalResult); // [ '^', index: 1, input: 'a^2' ]\n```\nThis is essentially a simple case of double-escaping. This module simply saves developers the trouble of determining the\nset of JavaScript's regular expression special characters and writing a function to escape them.\n\n## Development\n\nTo set up an optional development environment, a Vagrantfile is included. Install [Vagrant](https://www.vagrantup.com)\nand run:\n```sh\nvagrant up\n ```\nOnce Vagrant has completed provisioning, `vagrant ssh` into the box and run the tests with:\n```sh\nmake\n```\n\n## Feedback\n\nI wrote this miniature module to practice with a few of the tools, libraries, and workflows available to JS developers.\nI welcome constructive criticism and advice.\n","maintainers":[{"name":"monotonee","email":"monotonee@users.noreply.github.com"}],"time":{"modified":"2022-06-17T17:21:59.516Z","created":"2015-01-16T21:23:47.665Z","1.0.2":"2015-01-16T21:23:47.665Z","1.0.3":"2015-01-22T17:35:03.973Z","1.0.4":"2015-08-20T22:30:07.191Z","1.0.6":"2018-02-08T22:42:25.570Z"},"homepage":"https://github.com/monotonee/escape-regex-string/","keywords":["regex","RegExp","escape","string"],"repository":{"type":"git","url":"git+https://github.com/monotonee/escape-regex-string.git"},"author":{"name":"monotonee","email":"monotonee@tuta.io"},"bugs":{"url":"https://github.com/monotonee/escape-regex-string/issues"},"license":"MIT","readmeFilename":"README.md","users":{"arteffeckt":true}}