Open In App

Lodash _.escape() Method

Lodash _.escape() method is used to convert the characters “&”, “<“, “>”, ‘”‘, and “‘” of the given string into their corresponding HTML entities.

Syntax:

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

Parameter:

Return Value:

This method returns the escaped string.



Example 1: In this example, we are converting ‘<‘, ‘<‘, and ‘&’ by using the _.escape() method.




const _ = require('lodash');
 
let str1 = _.escape("Geeks << for >> Geeks");
console.log(str1);
 
let str2 = _.escape("GFG && Geeks");
console.log(str2);

Output:



"Geeks &lt;&lt; for &gt;&gt; Geeks"
"GFG &amp;&amp; Geeks"

Example 2: In this example, we are converting ‘&’, ‘+’, and ”(quote) by using the _.escape() method.




const _ = require('lodash');
 
let str1 = _.escape("Geeks & Geeks");
console.log(str1);
 
let str2 = _.escape("GFG '+' Geeks");
console.log(str2);
 
let str3 = _.escape("'Geeks'");
console.log(str3);

Output:

Geeks &amp; Geeks
GFG &#39;+&#39; Geeks
&#39;Geeks&#39;
Article Tags :