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:

  • string: This parameter holds the string containing escape characters.

Return Value:

This method returns the escaped string.

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

Javascript




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


Output:

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

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

Javascript




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


Output:

Beginner &amp; Beginner
GFG &#39;+&#39; Beginner
&#39;Beginner&#39;