将 unicode 字符替换为字符 (Javascript)
Replace unicode characters with characters (Javascript)
以下面的字符串为例:
“A profile of Mr. T, the A Team’s most well known member.”
如何使用 javascript 替换 unicode 字符编码并将其转换为以下内容:
"A profile of Mr. T, the A Team's most well known member."
@adeneo 使用 jQuery 发布了一个选项。这是我发现不使用 jQuery 的相关答案。来自这个答案:What's the right way to decode a string that has special HTML entities in it?
function parseHtmlEnteties(str) {
return str.replace(/&#([0-9]{1,4});/gi, function(match, numStr) {
var num = parseInt(numStr, 10); // read num as normal number
return String.fromCharCode(num);
});
}
以下面的字符串为例:
“A profile of Mr. T, the A Team’s most well known member.”
如何使用 javascript 替换 unicode 字符编码并将其转换为以下内容:
"A profile of Mr. T, the A Team's most well known member."
@adeneo 使用 jQuery 发布了一个选项。这是我发现不使用 jQuery 的相关答案。来自这个答案:What's the right way to decode a string that has special HTML entities in it?
function parseHtmlEnteties(str) {
return str.replace(/&#([0-9]{1,4});/gi, function(match, numStr) {
var num = parseInt(numStr, 10); // read num as normal number
return String.fromCharCode(num);
});
}