mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 11:27:50 +00:00
Ensure _.escapeRegExp escapes line terminators.
This commit is contained in:
@@ -94,11 +94,10 @@
|
|||||||
rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\n\\]|\\.)*?)\2)\]/g;
|
rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\n\\]|\\.)*?)\2)\]/g;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to match `RegExp` [special characters](http://www.regular-expressions.info/characters.html#special).
|
* Used to match `RegExp` [syntax characters](http://ecma-international.org/ecma-262/6.0/#sec-patterns)
|
||||||
* In addition to special characters the forward slash is escaped to allow for
|
* and those outlined by [`EscapeRegExpPattern`](http://ecma-international.org/ecma-262/6.0/#sec-escaperegexppattern).
|
||||||
* easier `eval` use and `Function` compilation.
|
|
||||||
*/
|
*/
|
||||||
var reRegExpChars = /[.*+?^${}()|[\]\/\\]/g,
|
var reRegExpChars = /[\/^$\\.*+?()[\]{}|\n\r\u2028\u2029]/g,
|
||||||
reHasRegExpChars = RegExp(reRegExpChars.source);
|
reHasRegExpChars = RegExp(reRegExpChars.source);
|
||||||
|
|
||||||
/** Used to match [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks). */
|
/** Used to match [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks). */
|
||||||
@@ -10491,7 +10490,7 @@
|
|||||||
function escapeRegExp(string) {
|
function escapeRegExp(string) {
|
||||||
string = baseToString(string);
|
string = baseToString(string);
|
||||||
return (string && reHasRegExpChars.test(string))
|
return (string && reHasRegExpChars.test(string))
|
||||||
? string.replace(reRegExpChars, '\\$&')
|
? string.replace(reRegExpChars, function(chr) { return '\\' + (stringEscapes[chr] || chr); })
|
||||||
: (string || '(?:)');
|
: (string || '(?:)');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
29
test/test.js
29
test/test.js
@@ -4222,29 +4222,31 @@
|
|||||||
QUnit.module('lodash.escapeRegExp');
|
QUnit.module('lodash.escapeRegExp');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
var escaped = '\\/\\^\\$\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\n\\r\\u2028\\u2029\\\\',
|
||||||
|
unescaped = '/^$.*+?()[]{}|\n\r\u2028\u2029\\';
|
||||||
|
|
||||||
test('should escape values', 1, function() {
|
test('should escape values', 1, function() {
|
||||||
var escaped = '\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\/\\\\',
|
strictEqual(_.escapeRegExp(unescaped + unescaped), escaped + escaped);
|
||||||
unescaped = '.*+?^${}()|[\]\/\\';
|
|
||||||
|
|
||||||
escaped += escaped;
|
|
||||||
unescaped += unescaped;
|
|
||||||
|
|
||||||
strictEqual(_.escapeRegExp(unescaped), escaped);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle strings with nothing to escape', 1, function() {
|
test('should handle strings with nothing to escape', 1, function() {
|
||||||
strictEqual(_.escapeRegExp('abc'), 'abc');
|
strictEqual(_.escapeRegExp('abc'), 'abc');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should return `"(?:)"` when provided nullish or empty string values', 3, function() {
|
||||||
|
strictEqual(_.escapeRegExp(null), '(?:)');
|
||||||
|
strictEqual(_.escapeRegExp(undefined), '(?:)');
|
||||||
|
strictEqual(_.escapeRegExp(''), '(?:)');
|
||||||
|
});
|
||||||
|
|
||||||
test('should work with `eval` and `Function`', 2, function() {
|
test('should work with `eval` and `Function`', 2, function() {
|
||||||
var string = '[lodash](https://lodash.com/)',
|
var actual = _.escapeRegExp(unescaped),
|
||||||
escaped = _.escapeRegExp(string),
|
regexp = eval('(/' + actual + '/)');
|
||||||
regexp = eval('(/' + escaped + '/)');
|
|
||||||
|
|
||||||
ok(regexp.test(string));
|
ok(regexp.test(unescaped));
|
||||||
|
|
||||||
regexp = Function('return /' + escaped + '/')();
|
regexp = Function('return /' + actual + '/')();
|
||||||
ok(regexp.test(string));
|
ok(regexp.test(unescaped));
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
@@ -17819,7 +17821,6 @@
|
|||||||
'camelCase',
|
'camelCase',
|
||||||
'capitalize',
|
'capitalize',
|
||||||
'escape',
|
'escape',
|
||||||
'escapeRegExp',
|
|
||||||
'kebabCase',
|
'kebabCase',
|
||||||
'pad',
|
'pad',
|
||||||
'padLeft',
|
'padLeft',
|
||||||
|
|||||||
Reference in New Issue
Block a user