简体   繁体   English

使用JavaScript将转义的html ASCII代码转换为纯文本

[英]Convert escaped html ASCII codes to plain text using JavaScript

I'm looking to convert a string of html entities specifying ASCII codes (ie: a) to the ASCII characters they represent (ie: a). 我想要将一串指定ASCII代码的html实体(即:a)转换为它们所代表的ASCII字符(即:a)。 I'm using a property of an object and trying to assign a value. 我正在使用对象的属性并尝试分配值。 For instance: 例如:

object.Text("");

When I pass is the string representing the entity, I get the same string back. 当我传递的是表示实体的字符串时,我得到相同的字符串。 I can't find the function to convert entities to the characters they represented. 我找不到将实体转换为它们所代表的字符的功能。

要将字符串中的所有数字字符实体转换为其等效字符,您可以执行以下操作:

str.replace(/&#(\d+);/g, function (m, n) { return String.fromCharCode(n); })

Try the String.fromCharCode() function. 尝试使用String.fromCharCode()函数。

alert(String.fromCharCode(97));

As you can see, you'll have to strip out the ampersand and pound sign. 如你所见,你将不得不去掉&符号和英镑符号。

Best regards... 最好的祝福...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM