Underscore.js escape and unescape functions

_.escape(string)

This function converts the characters &, <, >, “, `, and ‘ into their HTML escaped versions &amp;, &lt;, &gt;, &quot;, &#96; and &#x27;

<!DOCTYPE html>
<html>
    <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>

        <script type="text/javascript">
            var str = "<html>topjavatutorial.com</html>";
            var escapedStr = _.escape(str);
            console.log(escapedStr);
        </script>
    </head>

</html>

Output:

underscore escape

_.unescape(string)

The opposite of escape, replaces &amp;, &lt;, &gt;, &quot;, &#96; and &#x27; with their unescaped counterparts.

<!DOCTYPE html>
<html>
    <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>

        <script type="text/javascript">
            var escapedStr = "&lt;html&gt;topjavatutorial.com&lt;/html&gt;";
            var unescapedStr = _.unescape(escapedStr);
            console.log(unescapedStr);
        </script>
    </head>

</html>

underscore unescape

© 2017, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags