You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

文本转符文转换器双字母转单字符转换功能失效求助

问题:双字母转符文功能失效的Fuþark转换器修复

我正在实现一个文本转Fuþark符文的转换器,整体功能简单,空格、逗号转冒号的功能正常,但th=þ、ae=æ、oe=ø这三组双字母转单个符文的转换完全无法执行。我是编程新手,找不到问题所在。

现有代码

JavaScript代码

function generateCursiveText() {
  const cursiveDict = {"a":"ᚨ","b":"ᛒ","c":"ᚲ","d":"ᛞ","e":"ᛖ","f":"ᚠ","g":"ᚷ","h":"ᚺ","i":"ᛁ","j":"ᛃ","k":"ᚴ","l":"ᛚ",
    "m":"ᛗ","n":"ᚾ","o":"ᛟ","p":"ᛈ","q":"ᛩ","r":"ᚱ","s":"ᛋ","þ":"ᚦ","t":"ᛏ","u":"ᚢ","v":"ᚡ","w":"ᚹ",
    "x":"ᛪ","y":"ᚤ","z":"ᛉ","æ":"ᛇ","ä":"ᛇ","ø":"ᛟ","ö":"ᛟ","å":"ᚨ"," ":":",",":"};
  const textInput = document.getElementById("text-input").value.toLowerCase().trim();
  let output = "";
  for (let i = 0; i < textInput.length; i++) {
    if (cursiveDict[textInput[i]]) {
      output += cursiveDict[textInput[i]];
    } else {
      output += textInput[i];
    }
  }
  document.getElementById("output").innerHTML = output;
  //var clipboard = new ClipboardJS('.copy-it');
}

function replaceSpelling(input) {
  input = input.replace(/th/g, "þ");
  input = input.replace(/ae/g, "æ");
  input = input.replace(/oe/g, "ø");
  var lastChar = input[input.length - 1];
  return input;
}

HTML代码

<textarea id="text-input"></textarea>
<button type="button" onclick="generateCursiveText()">convert</button>
<textarea id="output"></textarea>

问题根源

你已经写了处理双字母替换的replaceSpelling函数,但在核心转换逻辑generateCursiveText里完全没有调用这个函数!当前代码直接拿原始输入的单个字符去匹配符文字典,根本没经过双字母替换的预处理步骤,自然不会生成对应的合并符文。

修复方案

修改generateCursiveText函数,在获取输入后先调用replaceSpelling做双字母替换,再进行符文转换:

function generateCursiveText() {
  const cursiveDict = {"a":"ᚨ","b":"ᛒ","c":"ᚲ","d":"ᛞ","e":"ᛖ","f":"ᚠ","g":"ᚷ","h":"ᚺ","i":"ᛁ","j":"ᛃ","k":"ᚴ","l":"ᛚ",
    "m":"ᛗ","n":"ᚾ","o":"ᛟ","p":"ᛈ","q":"ᛩ","r":"ᚱ","s":"ᛋ","þ":"ᚦ","t":"ᛏ","u":"ᚢ","v":"ᚡ","w":"ᚹ",
    "x":"ᛪ","y":"ᚤ","z":"ᛉ","æ":"ᛇ","ä":"ᛇ","ø":"ᛟ","ö":"ᛟ","å":"ᚨ"," ":":",",":"};
  // 先获取原始输入并预处理
  let textInput = document.getElementById("text-input").value.toLowerCase().trim();
  // 调用双字母替换函数处理输入
  textInput = replaceSpelling(textInput);
  let output = "";
  for (let i = 0; i < textInput.length; i++) {
    if (cursiveDict[textInput[i]]) {
      output += cursiveDict[textInput[i]];
    } else {
      output += textInput[i];
    }
  }
  document.getElementById("output").innerHTML = output;
  //var clipboard = new ClipboardJS('.copy-it');
}

function replaceSpelling(input) {
  input = input.replace(/th/g, "þ");
  input = input.replace(/ae/g, "æ");
  input = input.replace(/oe/g, "ø");
  // 原函数中lastChar变量未使用,可删除
  return input;
}

另外,原replaceSpelling函数里的lastChar变量没有实际用途,可以直接删除以减少冗余代码。

内容的提问来源于stack exchange,提问作者Hasse 'Hassan' Sørensen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 19:55:39