gpt4 book ai didi

javascript - JS - 幂符号/指数

转载 作者:行者123 更新时间:2023-12-03 11:17:09 27 4
gpt4 key购买 nike

有没有一种更简单的方法将幂符号/指数符号转换为其等价数字(即从 8),而不仅仅是一堆 replace是吗?

编辑:谢谢大家的解决方案!

最佳答案

您可以创建一个正则表达式并执行一次 replace 调用,为其提供一个回调函数:

let sup = {
"⁰": "0",
"¹": "1",
"²": "2",
"³": "3",
"⁴": "4",
"⁵": "5",
"⁶": "6",
"⁷": "7",
"⁸": "8",
"⁹": "9",
"⁺": "+",
"⁻": "-",
"⁼": "=",
"⁽": "(",
"⁾": ")",
"ⁿ": "n",
"ⁱ": "i"
};

let regex = RegExp(`[${Object.keys(sup).join("")}]`, "g");

// demo
let s = "x⁽ⁱ⁻²⁾ * y³";
let norm = s.replace(regex, c => sup[c]);
console.log(s);
console.log(norm);

关于javascript - JS - 幂符号/指数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61733331/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com