Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.escape() method is used to convert the characters “&”, “<“, “>”, ‘”‘, and “‘” of given string into their corresponding HTML entities.
Syntax:
_.escape([string=''])
Parameters: This method accepts single parameter as mentioned above and described below:
- string: This parameter holds the string containing escape characters.
Return Value: This method returns the escaped string.
Example 1:
Javascript
const _ = require( 'lodash' );
var str1 = _.escape( "Geeks << for >> Geeks" );
console.log(str1);
var str2 = _.escape( "GFG && Geeks" );
console.log(str2);
|
Output:
"Geeks << for >> Geeks"
"GFG && Geeks"
Example 2:
Javascript
const _ = require( 'lodash' );
var str1 = _.escape( "Geeks & Geeks" );
console.log(str1);
var str2 = _.escape( "GFG '+' Geeks" );
console.log(str2);
var str3 = _.escape( "'Geeks'" );
console.log(str3);
|
Output:
"Geeks & Geeks"
"GFG +' Geeks"
"'Geeks'"