Ensure _.escapeRegExp escapes line terminators.

This commit is contained in:
jdalton
2015-06-19 22:04:36 -07:00
parent 4896c725b6
commit 192667d65f
2 changed files with 19 additions and 19 deletions

View File

@@ -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 || '(?:)');
} }

View File

@@ -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',