├── LICENSE.md ├── README.md ├── RegExp.escape.js ├── RegExp.escape.min.js └── package.json /LICENSE.md: -------------------------------------------------------------------------------- 1 | # CC0 1.0 Universal License 2 | Public Domain Dedication 3 | 4 | The person(s) who associated a work with this deed has dedicated the work to the public domain by waiving all of his or her rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. 5 | 6 | You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission. 7 | 8 | In no way are the patent or trademark rights of any person affected by CC0, nor are the rights that other persons may have in the work or in how the work is used, such as publicity or privacy rights. 9 | 10 | Unless expressly stated otherwise, the person(s) who associated a work with this deed makes no warranties about the work, and disclaims liability for all uses of the work, to the fullest extent permitted by applicable law. 11 | 12 | When using or citing the work, you should not imply endorsement by the author or the affirmer. 13 | 14 | This is a [human-readable summary of the Legal Code](//creativecommons.org/publicdomain/zero/1.0/) ([read the full text](//creativecommons.org/publicdomain/zero/1.0/)). 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RegExp.escape 2 | 3 | **RegExp.escape** returns a string with escaped regular expression characters for use within a regular expression. 4 | 5 | ## Usage 6 | 7 | ```js 8 | var strName = 'Dr. Doogie Howser, M.D.'; 9 | 10 | var escName = RegExp.escape(strName); // Dr\. Doogie Howser, M\.D\. 11 | 12 | var regName = new RegExp('\\b' + escName + '\\b'); // /\bDr\. Doogie Howser, M\.D\.\b/ 13 | ``` 14 | 15 | ## Browser compatibility 16 | 17 | All modern browsers are supported, including: 18 | 19 | - Chrome 20 | - Internet Explorer 21 | - Firefox 22 | - Opera 23 | - Safari 24 | 25 | - Android 2.2+ 26 | - Blackberry 7+ 27 | - iOS Safari 4+ 28 | 29 | ## Prollyfill status 30 | 31 | If you would like to see **RegExp.escape** in a JavaScript standard, subscribe to the [ECMAScript List] and request to have it added it to an [ECMA Specification]. 32 | 33 | This project was inspired by [Stuart P. Bentley]’s [Specifiction Topic]. After reading it, I decided to throw this together. 34 | 35 | --- 36 | 37 | RegExp.escape.js is [323B](/RegExp.escape.js) or [107B](/RegExp.escape.min.js) minified and gzipped. 38 | 39 | [ECMAScript List]: https://mail.mozilla.org/listinfo/es-discuss 40 | [Specifiction Topic]: http://discourse.specifiction.org/t/regexp-escape-str/832 41 | [Anne van Kesteren]: https://twitter.com/annevk 42 | [Stuart P. Bentley]: https://twitter.com/stuartpb 43 | [ES6 Draft]: http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts 44 | -------------------------------------------------------------------------------- /RegExp.escape.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | // cache escapable characters RegExp 3 | var rEscapableCharacters = /[-\/\\^$*+?.()|[\]{}]/g; 4 | 5 | // cache escape + match String 6 | var sEscapeMatch = '\\$&'; 7 | 8 | // RegExp.escape 9 | RegExp.escape = function escape(string) { 10 | return String(string).replace(rEscapableCharacters, sEscapeMatch); 11 | }; 12 | })(); 13 | -------------------------------------------------------------------------------- /RegExp.escape.min.js: -------------------------------------------------------------------------------- 1 | !function(){var e=/[-\/\\^$*+?.()|[\]{}]/g,n="\\$&" 2 | RegExp.escape=function(c){return(c+"").replace(e,n)}}() -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "polyfill-regexp-escape", 3 | "version": "1.0.0", 4 | "description": "Return a string with escaped regular expression characters.", 5 | "main": "RegExp.escape.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/jonathantneal/regexp-escape.git" 12 | }, 13 | "keywords": [ 14 | "RegExp", 15 | "escape", 16 | "String", 17 | "JavaScript", 18 | "ECMA", 19 | "polyfill", 20 | "prollyfill" 21 | ], 22 | "author": "Jonathan Neal (http://jonathantneal.com)", 23 | "license": "CC0-1.0", 24 | "bugs": { 25 | "url": "https://github.com/jonathantneal/regexp-escape/issues" 26 | }, 27 | "homepage": "https://github.com/jonathantneal/regexp-escape" 28 | } 29 | --------------------------------------------------------------------------------