javascript函数将括号外的值添加到括号内的值

时间:2018-03-06 16:43:16

标签: javascript

我正在尝试创建一个自动创建问题的函数。

function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

var totalvar = getRandomInt(2,4);

var main = "";
    for (var i = 1; i <= totalvar; i++) {
        var test =  getRandomInt(1,3);
       // alert(test);
        var myArray = ["A","B","C","A&apos;","B&apos;","C&apos;"];
        var text ="";
        for (var a = 1; a <= test; a++) {
                    function random(array) {
                    return array[Math.floor(Math.random() * array.length)]
                    }
                        var testing = random(myArray);
                        if (testing =="A") {
                            var testing2 ="A&apos;";    

                        } else if (testing =="A&apos;") {
                            var testing2 ="A";  

                        } else if (testing =="B") {
                            var testing2 ="B&apos;";    

                        } else if (testing =="B&apos;") {
                            var testing2 ="B";  

                        }else if (testing =="C") {
                            var testing2 ="C&apos;";    

                        } else if (testing =="C&apos;") {
                            var testing2 ="C";  

                        }
                        //alert(testing);
                        //alert(myArray);
                        text += testing
                        var index = myArray.indexOf(testing);
                        if (index > -1) {
                            myArray.splice(index, 1);
                        }
                        var index = myArray.indexOf(testing2);
                        if (index > -1) {
                            myArray.splice(index, 1);
                        }

        }
         var brackets =  getRandomInt(1,3);
         var chances =  getRandomInt(1,3);
        var lastLetter = main.charAt(main.length - 1);

        if (brackets == 1) {
             text = "(" + text + ")";

         if (main == "") {
             main = text;
         }  else if ( lastLetter == ')') {
         if ( chances !== 1) {
             main += text;

         }else
            main += "+" + text;


         }else
         main += "+" + text;



         } else {
             if (main == "") {
             main = text;
         }   else if ( lastLetter == ')') {
                    if ( chances !== 1) {
                        main += text;

                    }else
                        main += "+" + text;


            }else
                main += "+" + text;


         }


}

我设法让它显示我想要的问题

B&#39; C&#39; +(A&#39; C&#39; B&#39;)+ BCA

B +(C&#39;)(CAB)

一个&#39; BC +(C&#39;)A + AB&#39;

B&#39; C&#39 + AB(A&#39; C&#39; B&#39)+ BCA

我被困了,因为我无法获得下一步的功能,它将括号外的值加倍

B&#39; C&#39; + A&#39; C&#39; B&#39; + BCA

B + C&#39; CAB

一个&#39; BC + C&#39; A + AB&#39;

B&#39; C&#39 + ABA&#39; C&#39; B&#39 + BCA

以上是我希望实现但我无法创建功能 任何提示家伙?

1 个答案:

答案 0 :(得分:1)

使用replace

&#13;
&#13;
var str = "B'C'+(A'C'B')+BCA";

var response = str.replace(/([\(\)]+)/g, '');

console.log(response);
// output: "B'C'+A'C'B'+BCA"
&#13;
&#13;
&#13;