javascript escape html

function escapeHtml(str) {
    return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
}

4
8
Awgiedawgie 440220 points

                                    function escapeHTML(text) {  
    var replacements= {&quot;&lt;&quot;: &quot;&amp;lt;&quot;, &quot;&gt;&quot;: &quot;&amp;gt;&quot;,&quot;&amp;&quot;: &quot;&amp;amp;&quot;, &quot;&quot;&quot;: &quot;&amp;quot;&quot;};                      
    return text.replace(/[&lt;&gt;&amp;&quot;]/g, function(character) {  
        return replacements[character];  
    }); 
}

4 (8 Votes)
0
3.5
2
Phoenix Logan 186125 points

                                    function escapeHtml(text) {
  return text
      .replace(/&amp;/g, &quot;&amp;&quot;)
      .replace(/&lt;/g, &quot;&lt;&quot;)
      .replace(/&gt;/g, &quot;&gt;&quot;)
      .replace(/&quot;/g, &quot;&quot;&quot;)
      .replace(/'/g, &quot;'&quot;);
}

3.5 (2 Votes)
0
3.56
9
Krish 100200 points

                                    //escaping HTML with jquery<br/>var dangerousHTML = &quot;&lt;script&gt;alert('Badabing Baby!');&lt;/script&gt;&quot;;<br/>$(&quot;#myElementID&quot;).text(dangerousHTML); //.text() function will escape and display text<br/><br/><br/>//Alternatively, here is plain Javascript escape function<br/>function escapeHtml(str) {<br/>    return str.replace(/&amp;/g, &quot;&amp;&quot;).replace(/&lt;/g, &quot;&lt;&quot;).replace(/&gt;/g, &quot;&gt;&quot;).replace(/&quot;/g, &quot;&quot;&quot;).replace(/'/g, &quot;'&quot;);<br/>}

3.56 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
html attribute escape in javascript js escape html tag javascript escape html text escape string for html htm js escape how to escape ' in javascript escape strings javascript js escape to html js htzml escape html escape function escape html special characters javascript escape html in javascript string how to auto escape html tags in javascript escape strings in javascript escape * in javascript javascript escape ` html special characters escape js escape string javascript html escape string javascript htm javascript escape - escape keyword in javascript escape html from string javascript escape &lt; escape string html how to escape string in html javascript escape string for html attribute create html escape string javascript escape ` function w3 js escape escape html from a string javascript replace characters with html escape escape ed in javascript escape html using javascript how to escape a ' javascript escape html to string javascript escape html javascript meaning javascript escape hypehn in string escape in html string javascript html escape string JS escape HTMLEntities how do you escape a . in javascript how to use escape in javascript html escape javascript escape character html escape jjs w3schools javascript escape string javascript escape string for html escape html tags in javascript how to escape html elements in string in javascript javascript script escape html entities js escape script tag escape string Javsscript how to escape html for javascript jquery escape string string html escape escape html code javascript escape htmo charter in jquert javascript html entities escape javascript to escape html special characters escape html string javascript escape function in javascript html escape string escape jquery.html escape js html escape characters html escape for { escape tags in jquery string escape html javascript function escape escape string in html javascript escape single quote js string escaper js use unescaped string escape html escape @ html js how to escape all html in a string escape html string html to string escape javacript escape value how to escape javascript funciton javascript html escape function javascrtip escape html script in html escape escape statements in js how to escape strings in javascript formatjs escape html tag escape html attribute in javascript javascript value = script tag escape characters html escape characters jquery html escape characters javascript html escape string javascript encode html escape html jquery jquery html encode text javascript escape function escape() html best way to pass special characters escape js &lt;%=%&gt; jquery escape html string escape \ in javascript javascript escape html special characters javascript string escape characters how to escape &quot; in html javascript escape tag sign in js escape parameters in javascript function \ need for escape letter in javascript marked js escape html jquery convert html %lt scape text javascript $ jquery encoding to html jquery escape function auto escape javascript string how to escape \ in javascript how to generalize escape string into html format in javascript javascript handle escape sequence in function javascript escape browser support nodejs html encode ingnore &lt;&gt;&quot; javascript encoding special characters escape a string javascript add escape characters to string javascript escape characters in javascript\ escape in javascript escape &quot;/&quot; in javascript option text not converting &amp;lt javascript escape html entities javasript escape &amp;#39; function to escape charachters in jquery escaping a / javascript javascript escaper js convert query special characters js encode html convert special characters to html in javascript escape html elements javascript escaping html in javascript javascript escape tags escape string js javascript escape \ js show escape characters js escape function javascript add escape characters add escape characters to string js encode special characters in javascript special characters javascript string escape characters javascript html escape function javascript js escape &lt; &quot; &quot; escaping characters js escape charecters in string js escape text javascript escape html characters in javascript \ escaping js escape javascript string escapehtml javascript js escape from html/js to string javascript special characters escape javascript escape() javascript + escape sequence escape html function Convert special characters to js javascript function to escape html characters html escape special characters javascript javascrtip HTML escaping javascript escaped text jquery HTML Entity Encode escape js &quot; js excape &amp;quot; js escape html special characters how to excape pramiters with js escape symbol javascript string to escape javascript js escape ( in string how to escape characters in javascript how to escape &quot; in javascipr string with special characters javascript how to escape this sign / in javascript how to escape / in javascript encode html entities jquery escape character javascript escape sequence in javascript htmlsafe jquery jquery sanitize string javascript escape characters in string javascript escape to html escape jquery javascript escape character esc html js how not to get a string in jquery special characters string javascript escaping special characters in javascript javascript escape cahracter how to escape a string in javascript js escape ; escapeHtml().replace javascript how to escape a string escape ' in javascript javascript string escape special characters js escape / js escape special characters jquery html escape javascript escape all html characters escape html tags javascript dec str.escapeHTML is not a function javascript string escaping how to escape // in http javascript string escape js escpaed js how to escape . in javascript escape character in javascript for + escape character in javascript how to excape &quot; in string javascript escape characters javascript how to escape special characters in js string format jaavscirpt escape ${} escape piece of text js escape ' JS how to cancel special charactera js javascript escape html brackets escape special charecters js \f in javascript escape escape html from text on jquery jquery un html escape how to escape all characters in javascript js html escape javascript how to escape \ escaper escapeHtml js html javascript escape html javascript escaped jquery encode html how to escape a character in javascript js escape $ jquery html escape code jquery html escape html jquery escape html code javascript escape string to html javascript string escape a character format string with escape characters javascript escape html characters javascript html escape characters js javascript string escaped escapehtml javascrip escape html js html escape string js input html escape javascript how to escape string in javascript Javascript escape &lt;&gt; jquery escape html javascript escape text for html string escape javascript jquery sanitize html jsescape javascript escape string escape ' on js file escape special characters javascript javascript escape \ in string js escape strings string escaping in javascript javascript string escape escape string in javascript js escape string escape html javascropt javascript escape html escape js escape js string javascript escape html string jquery encode html entities js &amp;lt;div&amp;gt; to &lt;div&gt; how to add escape character to string in javascript escape \in js escape javascript in html file javascript escape html characters javascript add escape characters to string escape html in js escapehtml js js how to escape &amp;code html special characters jquery js escape jquery html encode how to escape in javascript _.escape jquery escape html in javascript escape special characters javascript js html string escape variable escape method not working in node js javascript escape special characters js all characters to escape convert div into escape string jquery html encoding js escapable characters how to escape special characters in javascript string html escape javascript espcae html chars javascript javascript scape string escape js javascript escape characters how to escape your strings in javascript escape javascript escape mysql string javascript php escape string character jquery escape string quotes escape string javascript c# json string escape online jquery escape js escape html javascript escape html escape &quot; in jquery escape html javascript how to escape html in javascript javascript html escape escape javascript and display as text in html escape html javascript and display as text
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source