Underscore.js _.escape() function
Underscore.js is a library in javascript that makes operations on arrays, string, objects much easier and handy. _.escape() function is used to escape a special character string from inserting into HTML. Some of the strings that gets escape are “&“, “>“, “<“, “”“, etc.
Note: Some specials files are needed to be include while using this code directly in the browser. It is very necessary to link the underscore CDN before going and using underscore functions in the browser. When linking the underscore.js CDN The “_” is attached to the browser as a global variable.
Syntax:
_.escape(string);
Parameters: It takes only one parameter i.e the string.
Returns: It returns the string.
Few examples are given below for a better understanding of the function.
Example 1:
<!DOCTYPE html> < html > < head > < script src = </ script > </ head > < body > < script > let str="geeks for geeks & geeks"; let str2=_.escape(str) console.log(`Original string is: ${str}`) console.log(`New string is: ${str2}`) </ script > </ body > </ html > |
Output:
Example 2:
<!DOCTYPE html> <html> <head> <script src = </script> </head> <body> <script> console.log(`& is represented as: ${_.escape( "&" )}`) console.log(`, is represented as: ${_.escape( ", " )}`) console.log(`> is represented as: ${_.escape( ">" )}`) console.log(`< is represented as: ${_.escape( "<" )}`) console.log(` '' is represented as: ${_.escape( "''" )}`) </script> </body> </html> |
Output:
Please Login to comment...