Home  >  Q&A  >  body text

javascript - Regular matching of numbers after a specific statement in a string

var str = "总价为:1400.00元,单价为:200元"

How to use regular expressions or other methods to obtain the two fields 1400.00 and 200 and correspond to the total price and unit price?

迷茫迷茫2663 days ago1044

reply all(3)I'll reply

  • 欧阳克

    欧阳克2017-07-05 11:03:37

    var str = "总价为:1400.00元,单价为:200元"
    var matchResult = /总价为:([\d.]+?)元,单价为:([\d.]+?)元/.exec(str)
    if (matchResult) {
      let total = Number(matchResult[1])
      let unit = Number(matchResult[2])
      console.log(total, unit)
    }

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-07-05 11:03:37

    I think you can search it.

    reply
    0
  • 習慣沉默

    習慣沉默2017-07-05 11:03:37

    var pattern=/d+.d+/g;
    var matchs=str.match(pattern);
    console.log(matchs);

    reply
    0
  • Cancelreply