Requirements:

Backend return a string STR = “2123 + {3} * {4}”, extraction for the front end of the available an array (‘ 2123 ‘, ‘+’, ‘{3}’, ‘{4}’]

<script> const STR = "2123+{3}*{4}" const regx = /[+-/\*]/ const numArr = str.split(regx) console.log(numArr) // Extract numbers for(let i=1; i<numArr.length; i++) { const operatorIndex = str.indexOf(numArr[i]) -1 const operator = str.charAt(operatorIndex) console.log(operator) // Extract operator} </script>Copy the code

The printed result is:

Merge arrays:

<script> let finalArr = [] const str = "2123+{3}*{4}" const regx = /[+-/\*]/ const numArr = str.split(regx) finalArr = Parse (json.stringify (numArr)) // Deep copy console.log(numArr) // Extract numbers for(let I =1; i<numArr.length; i++) { const operatorIndex = str.indexOf(numArr[i]) -1 const operator = str.charAt(operatorIndex) console.log(operator) FinalArr. Splice (I, 0, operator)} console.log(finalArr) </script>Copy the code

The printed result is: