Syntax, Parameter and Return Value
Syntax:
_.escapeRegExp([string=''])
Parameters:
[string=''] (string): The string to escape.
Return Value:
(string) - Returns the escaped string.
Examples
Escaping a String for Use in RegEx:
JavaScript
var _ = require('lodash');
var str = 'Special characters: $.*+?^()[]{}|-';
console.log(_.escapeRegExp(str));

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Special characters: \$\.\*\+\?\^\(\)\[\]\{\}\|\-'
Demonstrates escaping a string containing special RegEx characters.
Dynamic Regular Expression Creation:
JavaScript
var userInput = 'file.exe';
var escapedInput = _.escapeRegExp(userInput);
var regex = new RegExp('^' + escapedInput + '$');
console.log(regex.test('file.exe'));
console.log(regex.test('file_exe'));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
false
Shows creating a regular expression from user input to match a specific filename.
Safe RegEx for User Input Matching:
JavaScript
var searchQuery = 'How much $ for a *?';
var escapedQuery = _.escapeRegExp(searchQuery);
var regex = new RegExp(escapedQuery);
console.log(regex.test('How much $ for a *?'));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
An example of safely using user input in a search functionality with RegEx.
Filtering Array Elements with Dynamic RegEx:
JavaScript
var filenames = ['report.doc', 'invoice.pdf', 'image.jpg'];
var userSearch = '.doc';
var escapedSearch = _.escapeRegExp(userSearch);
var regex = new RegExp(escapedSearch + '$');
var matchingFiles = filenames.filter(file => regex.test(file));
console.log(matchingFiles);

You can also try this code with Online Javascript Compiler
Run Code
Output:
['report.doc']
Demonstrates filtering an array based on a user-provided file extension.
Frequently Asked Questions
Which characters does _.escapeRegExp() escape?
_.escapeRegExp() escapes characters that have special meaning in regular expressions, such as ., *, +, ?, ^, (, ), [, ], {, }, |, and -.
Can _.escapeRegExp() be used for non-RegEx purposes?
While primarily designed for escaping strings for use in regular expressions, it can be used anytime you need to ensure special characters are treated as literals.
Is _.escapeRegExp() necessary for all RegEx operations?
_.escapeRegExp() is necessary when creating regular expressions from variable or user-generated strings. For static regular expressions with known content, it may not be needed.
Conclusion
Lodash's _.escapeRegExp() method is an essential tool for escaping special characters in strings when creating regular expressions dynamically. It ensures that variable data is treated as literal characters in pattern matching, enhancing the reliability and security of text processing operations.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.