开发者

how to convert &#(no); embedded in a string to a string with corresponding character in javascript?

开发者 https://www.devze.com 2023-01-01 02:07 出处:网络
how to convert &#(no); embedded in a string to a string with correspo开发者_如何学编程nding character in javascript?theString.replace(/&#x([0-9a-f]+);/ig, function(s, code) {

how to convert &#(no); embedded in a string to a string with correspo开发者_如何学编程nding character in javascript?


theString.replace(/&#x([0-9a-f]+);/ig, function(s, code) {
  return String.fromCharCode(parseInt(code, 16));
})

Similar for decimal (&#(\d+);).

For entities (©) you need a look-up table.


Why not just directly display the string as HTML anyway? Just strip all the < and > if you want to avoid XSS.

0

精彩评论

暂无评论...
验证码 换一张
取 消