简体   繁体   English

如何使用html char解析javascript中的字符串

[英]How parse string in javascript with html char

For example: I have this string var s = 'Some text .... ☕' 例如:我有这个字符串var s = 'Some text .... ☕' . How decode in JavaScript to var s = 'Some text .... \☕' ? 如何在JavaScript中解码为var s = 'Some text .... \☕'

Not sure what you have tried so far and not even that sure what exactly you want in your case, but… 不确定到目前为止您已经尝试过什么,甚至不确定自己想要什么,但是…

var s='Some text .... ☕';
s=s.replace(/&#(\d*);/g,function(full,number){
  return '\\u'+Number(number).toString(16);
});

returns "Some text .... \☕". 返回“ Some text .... \\ u2615”。

var s='Some text .... ☕';
s=s.replace(/&#(\d*);/g,function(full,number){
  return String.fromCharCode(Number(number));
});

returns "Some text .... ☕". 返回“ Some text ....☕”。

Instead of fromCharCode fromCodePoint is also possible. 代替fromCharCode fromCodePoint也可以。

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

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