Lodash _.escapeRegExp() Method
Last Updated :
25 Oct, 2023
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
Share your thoughts in the comments
Please Login to comment...