温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用javascript怎么对HTML字符进行转义

发布时间:2021-02-24 17:35:55 来源:亿速云 阅读:507 作者:Leah 栏目:web开发

这篇文章给大家介绍使用javascript怎么对HTML字符进行转义,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

1.背景:在项目中,经常遇到一些字符需要进行转义后才能显示到界面上,如“&”,在界面中显示的是“&”,在html中书写“&”,显示在界面的中的依然是“&”。

这时候,就需要进行转义

 使用javascript怎么对HTML字符进行转义

2.解决方案

<script>

var HtmlUtil = {

 /*1.用浏览器内部转换器实现html转码*/

 htmlEncode:function (html){

 //1.首先动态创建一个容器标签元素,如DIV

 var temp = document.createElement ("div");

 //2.然后将要转换的字符串设置为这个元素的innerText(ie支持)或者textContent(火狐,google支持)

 (temp.textContent != undefined ) ? (temp.textContent = html) : (temp.innerText = html);

 //3.最后返回这个元素的innerHTML,即得到经过HTML编码转换的字符串了

 var output = temp.innerHTML;

 temp = null;

 return output;

 },

 /*2.用浏览器内部转换器实现html解码*/

 htmlDecode:function (text){

 //1.首先动态创建一个容器标签元素,如DIV

 var temp = document.createElement("div");

 //2.然后将要转换的字符串设置为这个元素的innerHTML(ie,火狐,google都支持)

 temp.innerHTML = text;

 //3.最后返回这个元素的innerText(ie支持)或者textContent(火狐,google支持),即得到经过HTML解码的字符串了。

 var output = temp.innerText || temp.textContent;

 temp = null;

 return output;

 },

 /*3.用正则表达式实现html转码*/

 htmlEncodeByRegExp:function (str){ 

 var s = "";

 if(str.length == 0) return "";

 s = str.replace(/&/g,"&amp;");

 s = s.replace(/</g,"&lt;");

 s = s.replace(/>/g,"&gt;");

 s = s.replace(/ /g,"&nbsp;");

 s = s.replace(/\'/g,"'");

 s = s.replace(/\"/g,""");

 return s; 

 },

 /*4.用正则表达式实现html解码*/

 htmlDecodeByRegExp:function (str){ 

 var s = "";

 if(str.length == 0) return "";

 s = str.replace(/&amp;/g,"&");

 s = s.replace(/&lt;/g,"<");

 s = s.replace(/&gt;/g,">");

 s = s.replace(/&nbsp;/g," ");

 s = s.replace(/'/g,"\'");

 s = s.replace(/"/g,"\"");

 return s; 

 }

 };

</script>

使用方法:HtmlUtil.htmlDecodeByRegExp("&amp;")

PS:使用正则

使用正则转码:

var value = document.getElementById('input').value.trim();
 //对用户输入进行转义
 value = value.replace(/&/g,"&amp;");
 value = value.replace(/</g,"&lt;");
 value = value.replace(/>/g,"&gt;");
 value = value.replace(/ /g,"&nbsp;");
 value = value.replace(/"/g,'&quot;');

使用正则解码:

var value = e.target.innerText;
 // value = decodeURIComponent(value);
 value = value.replace(/&amp;/g,"&");
 value = value.replace(/&lt;/g,"<");
 value = value.replace(/&gt;/g,">");
 value = value.replace(/&nbsp;/g," ");
 value = value.replace(/&quot/g,"'");

下面是其他网友的补充

在编写html时,经常需要转义,才能正常显示在页面上。

并且,还可以防止xss。

解决方案:

一, 使用正则:

使用正则转码:

var value = document.getElementById('input').value.trim();
 //对用户输入进行转义
 value = value.replace(/&/g,"&amp;");
 value = value.replace(/</g,"&lt;");
 value = value.replace(/>/g,"&gt;");
 value = value.replace(/ /g,"&nbsp;");
 value = value.replace(/"/g,'&quot;');

使用正则解码:

var value = e.target.innerText;
// value = decodeURIComponent(value);
 value = value.replace(/&amp;/g,"&");
 value = value.replace(/&lt;/g,"<");
 value = value.replace(/&gt;/g,">");
 value = value.replace(/&nbsp;/g," ");
 value = value.replace(/&quot/g,"'");

关于使用javascript怎么对HTML字符进行转义就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI