Open In App

Lodash _.escapeRegExp() Method

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

Syntax:

_.escapeRegExp([string='']);

Parameters:

Return Value:

This method returns the escaped string.



Example 1: In this example, we are getting the escape string by the use of the _.escapeRegExp() method.




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

Output:



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

Example 2: In this example, we are getting the escape string by the use of the _.escapeRegExp() method.




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

Output:

/geeks/
/\(\?<geeks>\.\)\(\?<for>\.\)\(\?<geeks>\.\)/
\*\?\?\?\?\?\{\}\.
Article Tags :