Utf8编码在节点js中无法正常工作

时间:2016-06-20 07:38:54

标签: javascript node.js utf-8 html-entities

我在我的节点js sever:

中安装了utf8模块

这是我的代码

var utf8 = require('utf8');
console.log(utf8.encode('It’s a way of looking.'));
//output:- It’s a way of looking.

它没有像php那样产生相同的输出:

echo utf8_encode('It’s a way of looking.');
//output :- It’s a way of looking.

这是节点js中utf8模块中的错误还是我做错了什么?

2 个答案:

答案 0 :(得分:2)

’是一个HTML实体。如果您未将输出视为HTML,则会将其视为’。如果要在JavaScript中输出特定字符代码,请使用JavaScript转义序列:

console.log(utf8.encode('It\u2019s a way of looking.'));

答案 1 :(得分:0)

这不是utf8编码问题,这是一个html实体问题

这是代码: -

 var Entities = require('html-entities').XmlEntities;

    entities = new Entities();

    console.log(entities.decode('It’s a way of looking.'));
    //output:- It’s a way of looking.

感谢@JLRishe指出它。

相关问题