how do you do html encode using javascript? [duplicate]

一世执手 提交于 2019-12-17 22:31:35

问题


Possible Duplicate:
JavaScript/jQuery HTML Encoding

I have html tags need to be encoded.

<b>test</b>

I need to encode it to :

&lt;b&gt;test&lt;/b&gt;

I am using escape, but it doesn't work.

document.write(escape("<b>test</b>"));

the result I got is

%3Cb%3Etest%3C/b%3E

this is not what I expected. is there another way to do html encode using javascript?


回答1:


var html = "<b>test</b>";
var result = html.replace(/</g,"&lt;").replace(/>/g,"&gt;");


来源:https://stackoverflow.com/questions/14346414/how-do-you-do-html-encode-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!