gpt4 book ai didi

javascript - 使用 native 除法运算符对解码的 URI 十六进制代码使用 Replace()

转载 作者:行者123 更新时间:2023-11-28 18:33:42 24 4
gpt4 key购买 nike

构建一个计算器。

var process = "6÷6";  // need to replace division sign with one that javascript can evaluate with
process = encodeURI(process);
process.replace(/%C3%B7/gi,'/'); // replacement step that doesn't work - %C3%B7 is what shows up as the hex divison sign in chrome debugger, not sure why
process = decodeURI(process);
result = eval(process);

最佳答案

您可以创建一个属性设置为算术运算符的对象。请注意,.replace() 可能不是必需的

var map = {"÷":"/"};
var operatorType = "÷";
var process = "6" + map[operatorType] + "6"; // need to replace division sign with one that javascript can evaluate with
process = encodeURI(process);
process.replace(/%C3%B7/gi,'/'); // replacement step that doesn't work - %C3%B7 is what shows up as the hex divison sign in chrome debugger, not sure why
process = decodeURI(process);
result = eval(process);
document.body.innerHTML = result;

关于javascript - 使用 native 除法运算符对解码的 URI 十六进制代码使用 Replace(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37504218/

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