From 61251b305ddfc043ab565d01f948c61c2d7dfadd Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 31 Jul 2015 14:02:59 -0700 Subject: [PATCH] Simplify `_.escapeRegExp` to align with the defunct ES7 proposal. --- lodash.js | 15 +++++++-------- test/test.js | 38 ++++---------------------------------- 2 files changed, 11 insertions(+), 42 deletions(-) diff --git a/lodash.js b/lodash.js index bae836735..ca337d8ad 100644 --- a/lodash.js +++ b/lodash.js @@ -94,10 +94,9 @@ rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\n\\]|\\.)*?)\2)\]/g; /** - * Used to match `RegExp` [syntax characters](http://ecma-international.org/ecma-262/6.0/#sec-patterns) - * and those outlined by [`EscapeRegExpPattern`](http://ecma-international.org/ecma-262/6.0/#sec-escaperegexppattern). + * Used to match `RegExp` [syntax characters](http://ecma-international.org/ecma-262/6.0/#sec-patterns). */ - var reRegExpChars = /^[:!,]|[\\^$.*+?()[\]{}|\/]|(^[0-9a-fA-Fnrtuvx])|([\n\r\u2028\u2029])/g, + var reRegExpChars = /[\\^$.*+?()[\]{}|]/g, reHasRegExpChars = RegExp(reRegExpChars.source); /** Used to match [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks). */ @@ -9805,8 +9804,8 @@ } /** - * Escapes the `RegExp` special characters "\", "/", "^", "$", ".", "|", "?", - * "*", "+", "(", ")", "[", "]", "{" and "}" in `string`. + * Escapes the `RegExp` special characters "^", "$", "\", ".", "*", "+", + * "?", "(", ")", "[", "]", "{", "}", and "|" in `string`. * * @static * @memberOf _ @@ -9816,13 +9815,13 @@ * @example * * _.escapeRegExp('[lodash](https://lodash.com/)'); - * // => '\[lodash\]\(https:\/\/lodash\.com\/\)' + * // => '\[lodash\]\(https://lodash\.com/\)' */ function escapeRegExp(string) { string = baseToString(string); return (string && reHasRegExpChars.test(string)) - ? string.replace(reRegExpChars, escapeRegExpChar) - : (string || '(?:)'); + ? string.replace(reRegExpChars, '\\$&') + : string; } /** diff --git a/test/test.js b/test/test.js index d2add8bc1..580072c4f 100644 --- a/test/test.js +++ b/test/test.js @@ -287,15 +287,6 @@ new URIError ]; - /** Used to check escaped regexp characters. */ - var regexpEscapes = { - '0': 'x30', '1': 'x31', '2': 'x32', '3': 'x33', '4': 'x34', - '5': 'x35', '6': 'x36', '7': 'x37', '8': 'x38', '9': 'x39', - 'A': 'x41', 'B': 'x42', 'C': 'x43', 'D': 'x44', 'E': 'x45', 'F': 'x46', - 'a': 'x61', 'b': 'x62', 'c': 'x63', 'd': 'x64', 'e': 'x65', 'f': 'x66', - 'n': 'x6e', 'r': 'x72', 't': 'x74', 'u': 'x75', 'v': 'x76', 'x': 'x78' - }; - /** Used to check whether methods support typed arrays. */ var typedArrays = [ 'Float32Array', @@ -3609,41 +3600,20 @@ QUnit.module('lodash.escapeRegExp'); (function() { - var escaped = '\\/\\^\\$\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\n\\r\\u2028\\u2029\\\\', - unescaped = '/^$.*+?()[]{}|\n\r\u2028\u2029\\'; + var escaped = '\\^\\$\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\\\', + unescaped = '^$.*+?()[]{}|\\'; test('should escape values', 1, function() { strictEqual(_.escapeRegExp(unescaped + unescaped), escaped + escaped); }); - test('should escape special characters at the start of a string', 1, function() { - var chars = [ - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - 'a', 'b', 'c', 'd', 'e', 'f', - 'A', 'B', 'C', 'D', 'E', 'F', - 'n', 'r', 't', 'u', 'v', 'x', - ':', '!', ',' - ]; - - var expected = _.map(chars, function(chr) { - var escaped = '\\' + (regexpEscapes[chr] || chr); - return [escaped, escaped + 'z', 'z' + chr]; - }); - - var actual = _.map(chars, function(chr) { - return [_.escapeRegExp(chr), _.escapeRegExp(chr + 'z'), _.escapeRegExp('z' + chr)]; - }); - - deepEqual(actual, expected); - }); - test('should handle strings with nothing to escape', 1, function() { strictEqual(_.escapeRegExp('ghi'), 'ghi'); }); - test('should return `"(?:)"` when provided nullish or empty string values', 1, function() { + test('should return an empty string for empty values', 1, function() { var values = [, null, undefined, ''], - expected = _.map(values, _.constant('(?:)')); + expected = _.map(values, _.constant('')); var actual = _.map(values, function(value, index) { return index ? _.escapeRegExp(value) : _.escapeRegExp();