Tuesday, November 25, 2025
HomeLanguagesJavascriptLodash _.escapeRegExp() Method

Lodash _.escapeRegExp() Method

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("/neveropen/");
console.log(str1);
  
var str2 = _.escapeRegExp("/(?<neveropen>.)(?<for>.)(?<neveropen>.)/");
console.log(str2);
  
var str3 = _.escapeRegExp("\*?????{}.");
console.log(str3);


Output:

"/neveropen/"
"/\\(\\?<neveropen>\\.\\)\\(\\?<for>\\.\\)\\(\\?<neveropen>\\.\\)/"
"\\*\\?\\?\\?\\?\\?\\{\\}\\."
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32412 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6790 POSTS0 COMMENTS
Nicole Veronica
11934 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6912 POSTS0 COMMENTS
Ted Musemwa
7169 POSTS0 COMMENTS
Thapelo Manthata
6868 POSTS0 COMMENTS
Umr Jansen
6856 POSTS0 COMMENTS