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>\.\)/
\*\?\?\?\?\?\{\}\.
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
25 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...