diff --git a/lodash.src.js b/lodash.src.js index 7851cc13e..c3dd1dc95 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -114,9 +114,9 @@ /** * Used to match `RegExp` special characters. - * See this [article on `RegExp` characters](http://www.regular-expressions.info/characters.html#special) - * for more details. In addition the forward slash is escaped to allow for - * easier `eval` and `Function` compilation use. + * See this [article on `RegExp` special characters](http://www.regular-expressions.info/characters.html#special) + * for more details. In addition to special characters the forward slash is + * escaped to allow for easier `eval` use and `Function` compilation. */ var reRegExpChars = /[.*+?^${}()|[\]\/\\]/g, reHasRegExpChars = RegExp(reRegExpChars.source); diff --git a/test/test.js b/test/test.js index 00a5a3ffc..3f173cc56 100644 --- a/test/test.js +++ b/test/test.js @@ -4195,19 +4195,30 @@ QUnit.module('lodash.escapeRegExp'); (function() { - var escaped = '\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\/\\\\', - unescaped = '.*+?^${}()|[\]\/\\'; - - escaped += escaped; - unescaped += unescaped; - test('should escape values', 1, function() { + var escaped = '\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\/\\\\', + unescaped = '.*+?^${}()|[\]\/\\'; + + escaped += escaped; + unescaped += unescaped; + strictEqual(_.escapeRegExp(unescaped), escaped); }); test('should handle strings with nothing to escape', 1, function() { strictEqual(_.escapeRegExp('abc'), 'abc'); }); + + test('should work with `eval` and `Function`', 2, function() { + var string = '[lodash](https://lodash.com/)', + escaped = _.escapeRegExp(string), + regexp = eval('(/' + escaped + '/)'); + + ok(regexp.test(string)); + + regexp = Function('return /' + escaped + '/')(); + ok(regexp.test(string)); + }); }()); /*--------------------------------------------------------------------------*/