mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 23:57:49 +00:00
Simplify _.escapeRegExp to align with the defunct ES7 proposal.
This commit is contained in:
15
lodash.js
15
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
38
test/test.js
38
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();
|
||||
|
||||
Reference in New Issue
Block a user