Skip to content
Related Articles

Related Articles

Lodash _.escapeRegExp() Method

Improve Article
Save Article
Like Article
  • Last Updated : 07 Sep, 2020

The _.escapeRegExp() method is used to escape the Regular Expression special characters “^”, “$”, “”, “.”, “*”, “+”, “?”, “(“, “)”, “[“, “]”, “{“, “}”, and “|” in string.

Syntax:

_.escapeRegExp([string=''])

Parameters: This method accepts single parameter as mentioned above and described below:

  • string: This parameter holds the string to escape.

Return Value: This method returns the escaped string.

Example 1:

Javascript




const _ = require('lodash'); 
  
var str1 = _.escapeRegExp("/a/");
console.log(str1);
  
var str2 = _.escapeRegExp("\*?{}.");
console.log(str2);

Output:

"/a/"
"\\*\\?\\{\\}\\."

Example 2:

Javascript




const _ = require('lodash'); 
  
var str1 = _.escapeRegExp("/geeks/");
console.log(str1);
  
var str2 = _.escapeRegExp("/(?<geeks>.)(?<for>.)(?<geeks>.)/");
console.log(str2);
  
var str3 = _.escapeRegExp("\*?????{}.");
console.log(str3);

Output:

"/geeks/"
"/\\(\\?<geeks>\\.\\)\\(\\?<for>\\.\\)\\(\\?<geeks>\\.\\)/"
"\\*\\?\\?\\?\\?\\?\\{\\}\\."
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!