简体   繁体   中英

Converting unicode character to string format

有谁知道如何将 unicode 转换为 javascript 中的字符串。例如:

\∑ -> ∑<\/code> \2 -> 2<\/code> \∫ -> ∫<\/code>

我基本上希望能够在 xhtml 或 html 中显示符号。还没有决定我将使用哪个。

"

A function from k.ken's response:

function unicodeToChar(text) {
   return text.replace(/\\u[\dA-F]{4}/gi, 
          function (match) {
               return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16));
          });
}

Takes all unicode characters in the inputted string, and converts them to the character.

Just found a way: String.fromCharCode(parseInt(unicode,16)) returns the right symbol representation. The unicode here doesn't have the \\u\u003c/code> in front of it just the number.

To convert an given Unicode-Char like  to String-Representation, you can also use this one-liner:

var unicodeToStr = ''.codePointAt(0).toString(16)

The above example gives you 'F21D'. Used with fontAwesome, you get street-view Icon: '\\F21D'

Another way:

const unicodeText = "F1A3";
let unicodeChar = JSON.parse(`["\\u${unicodeText}"]`)[0];
var string = '/0004';  // One of unicode
var unicodeToStr = string.codePointAt(0).toString(16)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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