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

时间:2021-10-10 00:29:12

I'm looking to convert a string of html entities specifying ASCII codes (ie: a) to the ASCII characters they represent (ie: a). I'm using a property of an object and trying to assign a value. For instance:

我希望将一串指定ASCII代码(即:a)的html实体转换为它们所代表的ASCII字符(即:a)。我正在使用对象的属性并尝试分配值。例如:

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.

当我传递的是表示实体的字符串时,我得到相同的字符串。我找不到将实体转换为它们所代表的字符的功能。

3 个解决方案

#1


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...

#2


To convert all numerical character entities in a string to their character equivalents you can do this:

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

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

#1


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...

#2


To convert all numerical character entities in a string to their character equivalents you can do this:

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

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

#3