如何将数字转换为unicode粗体版本?

时间:2017-06-22 09:17:20

标签: javascript unicode

我想在javascript确认弹出窗口中加粗一些字符。由于你不能在那里使用html / css,并将解决方案扩展到underline with the dedicated unicode character,我想我可能只是将每个digit with its bolded unicode counterpart替换为they are five digit long(范围从1D7CE)到,1D7D7),我带来了以下代码:

function bold(number) {
  return number.
    replace(/0/, String.fromCharCode(0x1D7CE)).
    replace(/1/, String.fromCharCode(0x1D7CF)).
    replace(/2/, String.fromCharCode(0x1D7D0)).
    replace(/3/, String.fromCharCode(0x1D7D1)).
    replace(/4/, String.fromCharCode(0x1D7D2)).
    replace(/5/, String.fromCharCode(0x1D7D3)).
    replace(/6/, String.fromCharCode(0x1D7D4)).
    replace(/7/, String.fromCharCode(0x1D7D5)).
    replace(/8/, String.fromCharCode(0x1D7D6)).
    replace(/9/, String.fromCharCode(0x1D7D7));
}

但我必须误解某些内容,例如1被替换为(D7CE)。

什么是可行的解决方案?

1 个答案:

答案 0 :(得分:4)

试试这个简化的实现:



function bold(number) {
  return number.replace(/\d/g, (c) => String.fromCodePoint(0x1D79E + c.charCodeAt(0)));
}

console.log(bold('abc 0123456789 def'));




这使用String.fromCodePoint()支持全范围的unicode,unlike String.fromCharCode()

这种简化利用了这样的事实:数字和粗体数字都连续运行10个unicode点,相隔0x1D79E的距离。