How to convert string equation to number in javascript?

前端 未结 5 995
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-20 03:04

How to convert string equation to number in javascript?

Say I have \"100*100\" or \"100x100\" how do I evaluate that and convert to a number?

5条回答
  •  暖寄归人
    2020-12-20 03:46

    This will give you the product whether the string is using * or x:

    var str = "100x100";
    
    var tmp_arr = str.split(/[*x]/);
    var product = tmp_arr[0]*tmp_arr[1];   // 10000
    

提交回复
热议问题