【问题标题】:Need a way to set variables of a string using an array需要一种使用数组设置字符串变量的方法
【发布时间】:2020-12-09 16:02:45
【问题描述】:

我的问题与上面描述的完全一样。我在下面有一些代码定义了数组中设置的变量。已经存在的代码只是删除了文本框中定义变量的区域,以便您得到答案。您可以只定义变量,也可以在定义变量后编写 [simp] 以获得简化的答案,而不仅仅是交换变量。例如,我希望你能够在文本框中写 1+a[a=1],并在下面的文本中得到 1+1。

<!DOCTYPE html>
    <html>
        <head>
            <title>PlayBox</title>
            <style>
                textarea {
                    resize: none;
                    width: 100%;
                    height: 100px;
                }
                body{
                    font-family: 'Courier New', Courier, monospace;
                    background-color: white;
                }
            </style>
        </head>
        <body>
            <textarea id="input"></textarea>
            <button onclick="run()">Run</button>
            <p id="output"></p>
            <script>
                function run() {
                    var input = document.getElementById('input').value 
                    var simpPositive = input.search('[simp]')
                    var doctorPositive = input.search('[doctor]')
                    var defineVarPositive = input.search(/\[\D\=\d/)
                    if ((simpPositive > -1) & (defineVarPositive < 0)) {
                        var newInput1 = input.replace('[simp]', '').replace(/(?<=[\d\)])\((?=\d)/g, "*(")
                        var simp = eval(newInput1)
                        document.getElementById('output').innerHTML = simp
                    }
                    if (doctorPositive > -1) {}
                    if (defineVarPositive > -1) {
                        var findMultVar = input.split('[')
                        var elem = findMultVar[1];
                        var fixFindMultVar = elem.substring(0, elem.length -1)
                        var ffmvarr = fixFindMultVar.split(';')//array here
                        var replaceletter = input.split('[')[0]//need more code here to change the variables using the array
                        console.log(replaceletter)
                        if (simpPositive > -1) {
                            var deleteSimp = replaceletter.replace('[simp]', '')
                            var newInput2 = deleteSimp.replace(/(?<=[\d\)])\((?=\d)/g, "*(")
                            var simp = eval(newInput2)
                            document.getElementById('output').innerHTML = simp
                        } else {
                            document.getElementById('output').innerHTML = replaceletter
                        }
                    }
                    if ((simpPositive < 0) & (defineVarPositive < 0) & (doctorPositive < 0)) {
                        document.getElementById('output').innerHTML = 'error: no modifiers found';
                    }
                }
            </script>
        </body>
    </html>

【问题讨论】:

    标签: javascript


    【解决方案1】:

    我可以看到您尝试使用split 来解决此问题。尽管理论上可能有解决方案,但它会非常混乱。正如您已经在代码中的其他地方所做的那样,只需使用字符串匹配和替换即可轻松解决。

    下面是一个简单的解决方案,它将搜索所有变量分配(即[variable=value]),然后在input 上执行全局查找和替换。最后,代码删除所有变量赋值,给你想要的结果。

    它应该适用于任何数量的变量分配,以及任何数量的变量使用(感谢全局替换)。

    for (const match of input.match(/(\[\D\=\d\])/g)) {
      const name = match[1]
      const val = match[3];
      input = input.replace(new RegExp(name, 'g'), val);
    }
    input = input.replace(/\[.+?\]/g, '');
    

    注意:我不确定您的全部要求 - 但目前您的初始代码(以及上面的代码)占单个字符变量名称和单个数字值;这很容易适应更一般的用例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 2013-12-07
      • 1970-01-01
      • 2017-03-15
      相关资源
      最近更新 更多