我想将文本转换为 unicode 粗体格式。
我发现了
如何使用 unicode 将文本转换为粗体大写字母?
谢谢
这是一个快速代码片段,可以满足您的要求:
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); // -> "𝗧𝗵𝗶𝘀 𝗶𝘀 𝗮 𝘁𝗲𝘅𝘁: 𝗔𝗕𝗖"
参考资料:
无正则表达式的答案,灵感来自@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")
晚上好,
我们使用translate函数将字母和数字字符直接转换为粗体。
我们需要对重音字符使用相同的函数。
您能告诉我们这方面如何进行吗?
谢谢你