将 Unicode 字符样式化为纯文本

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

我有这段代码可以将纯文本转换为 Unicode 样式的字符:

function convertText(text, style) {

  // 1. Convert the text to the bold type with the unicode.
  const conv = {
    c: function(text, obj) {return text.replace(new RegExp(`[${obj.reduce((s, {r}) => s += r, "")}]`, "g"), e => {
      const t = e.codePointAt(0);
      if ((t >= 48 && t <= 57) || (t >= 65 && t <= 90) || (t >= 97 && t <= 122)) {
        return obj.reduce((s, {r, d}) => {
          if (new RegExp(`[${r}]`).test(e)) s = String.fromCodePoint(e.codePointAt(0) + d);
          return s;
        }, "")
      }
      return e;
    })},
    bold: function(text) {return this.c(text, [{r: "0-9", d: 120734}, {r: "A-Z", d: 120211}, {r: "a-z", d: 120205}])},
    italic: function(text) {return this.c(text, [{r: "A-Z", d: 120263}, {r: "a-z", d: 120257}])},
    boldItalic: function(text) {return this.c(text, [{r: "A-Z", d: 120315}, {r: "a-z", d: 120309}])},
  };

  if(style == 'bold')
    return(conv.bold(text));
  else if(style == 'italic')
    return(conv.italic(text));
  else if(style == 'bolditalic')
    return(conv.boldItalic(text));
  else
    return text;
}

我在 StackOverflow 链接上找到了它: 带样式的 Google 脚本字符串

我尝试逆向代码以将样式化的 unicode 字符转换为纯文本,但失败了。希望有人可以反转代码以从样式化的 unicode 字符中获取纯文本。

javascript google-apps-script unicode converters
1个回答
0
投票

虽然我不确定是否能正确理解你的预期结果,但是下面的示例脚本怎么样?

示例脚本:

在此示例中,使用了

normalize()

function convertText(text, style) {

  // 1. Convert the text to the bold type with the unicode.
  const conv = {
    c: function (text, obj) {
      return text.replace(new RegExp(`[${obj.reduce((s, { r }) => s += r, "")}]`, "g"), e => {
        const t = e.codePointAt(0);
        if ((t >= 48 && t <= 57) || (t >= 65 && t <= 90) || (t >= 97 && t <= 122)) {
          return obj.reduce((s, { r, d }) => {
            if (new RegExp(`[${r}]`).test(e)) s = String.fromCodePoint(e.codePointAt(0) + d);
            return s;
          }, "")
        }
        return e;
      })
    },
    bold: function (text) { return this.c(text, [{ r: "0-9", d: 120734 }, { r: "A-Z", d: 120211 }, { r: "a-z", d: 120205 }]) },
    italic: function (text) { return this.c(text, [{ r: "A-Z", d: 120263 }, { r: "a-z", d: 120257 }]) },
    boldItalic: function (text) { return this.c(text, [{ r: "A-Z", d: 120315 }, { r: "a-z", d: 120309 }]) },
  };

  if (style == 'bold')
    return (conv.bold(text));
  else if (style == 'italic')
    return (conv.italic(text));
  else if (style == 'bolditalic')
    return (conv.boldItalic(text));
  else
    return text;
}

// Please run this function.
function sample() {
  const srcText = "Sample Text";

  const t1 = convertText(srcText, "italic");
  const t2 = convertText(srcText, "bold");
  const t3 = convertText(srcText, "bolditalic");

  const text1 = Array(3).fill(srcText).join("");
  const text2 = t1 + t2 + t3;
  const text3 = text2.normalize("NFKC");

  console.log({ text1, text2, text3 });
}

运行该脚本,得到如下结果。在这种情况下,我认为

NFKC
NFKD
可能可以使用。

{
  "text1":"Sample TextSample TextSample Text",
  "text2":"𝘚𝘢𝘮𝘱𝘭𝘦 𝘛𝘦𝘹𝘵𝗦𝗮𝗺𝗽𝗹𝗲 𝗧𝗲𝘅𝘁𝙎𝙖𝙢𝙥𝙡𝙚 𝙏𝙚𝙭𝙩",
  "text3":"Sample TextSample TextSample Text"
}

在此示例输出中,原始文本将转换为

italic
bold
bolditalic
,并且转换后的文本也会转换为普通文本。
text1
text2
text1
分别是源文本、转换后的文本以及转换后的文本转换成的普通文本。

参考:

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