Open In App

Lodash _.escapeRegExp() Method

Last Updated : 25 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Syntax:

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

Parameters:

  • string: This parameter holds the string to escape.

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.

Javascript




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.

Javascript




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>\.\)/
\*\?\?\?\?\?\{\}\.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads