Javascript 将文本转换为粗体 unicode 字符集

问题描述 投票:0回答:3

我想将文本转换为 unicode 粗体格式。

我发现了

  • [U+1D5D4 - U+1D5ED] 为粗体大写字母 [A - Z]
  • [U+1D5EE - U+1D607] 为粗体大写字母 [a - z]

如何使用 unicode 将文本转换为粗体大写字母?

谢谢

javascript unicode unicode-string
3个回答
8
投票

这是一个快速代码片段,可以满足您的要求:

let text = "This is a text: ABC";
function translate (char)
{
    let diff;
    if (/[A-Z]/.test (char))
    {
        diff = "𝗔".codePointAt (0) - "A".codePointAt (0);
    }
    else
    {
        diff = "𝗮".codePointAt (0) - "a".codePointAt (0);
    }
    return String.fromCodePoint (char.codePointAt (0) + diff);
}
let newText = text.replace (/[A-Za-z]/g, translate);
console.log (text);
console.log (newText); // -> "𝗧𝗵𝗶𝘀 𝗶𝘀 𝗮 𝘁𝗲𝘅𝘁: 𝗔𝗕𝗖"

参考资料:


0
投票

无正则表达式的答案,灵感来自@Albert Stein解决方案:

const upperDiff = "𝗔".codePointAt(0) - "A".codePointAt(0);
const lowerDiff = "𝗮".codePointAt(0) - "a".codePointAt(0);

const isUpper = (n) => n >= 65 && n < 91;
const isLower = (n) => n >= 97 && n < 123;

const bolderize = (char) => {
  const n = char.charCodeAt(0);
  if (isUpper(n)) return String.fromCodePoint(n + upperDiff);
  if (isLower(n)) return String.fromCodePoint(n + lowerDiff);
  return char;
};

const bolderizeWord = (word) => [...word].map(bolderize).join("");

console.log("a" + bolderize("a"))
console.log(bolderizeWord("Bold") + " move")


0
投票

晚上好,

我们使用translate函数将字母和数字字符直接转换为粗体。

我们需要对重音字符使用相同的函数。

您能告诉我们这方面如何进行吗?

谢谢你

© www.soinside.com 2019 - 2024. All rights reserved.