Lodash _.escapeRegExp() Method
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( "/Beginner/" ); console.log(str1); let str2 = _.escapeRegExp( "/(?<Beginner>.)(?<for>.)(?<Beginner>.)/" ); console.log(str2); let str3 = _.escapeRegExp( "\*?????{}." ); console.log(str3); |
Output:
/Beginner/
/\(\?<Beginner>\.\)\(\?<for>\.\)\(\?<Beginner>\.\)/
\*\?\?\?\?\?\{\}\.