简体   繁体   中英

How to keep the currency symbol in the right position from free text?

Suppose I have the function presented below to extract prices from free text. Where prices sometimes have the currency symbol in a different position, for example, "19€99". How could I rewrite or change the way this algorithm works to accommodate this new requirement and future such requirements?

var valid_countries = ["US", "IT"]

function extractPrice(description, country){

  if (!valid_countries.includes(country)){
    return -1
  }

  if (country == "US") {
    var price = description.match(/\$(\d+\.\d+)/)[1]

    if (price == null) {
      return -1
    }

    return parsreFloat(price)
  }

  if (country == "IT") {
    var price = description.match(/\€(\d+\.\d+)/)[1]

    if (price == null) {
      return -1
    }

    return parsreFloat(price)
  }


}

I think you can use this for all currencies. This is working well in crawling websites. Also easily read, modify and can be improved in my opinion. You can add your extractPrice function and return with formPrice .

 let priceTexts = [ "19€99", "19,99€", "19.99€", "19,€99", "19$99", "19,99$", "19.99$", "19,$99", "€19.99", "$19.99", "CHF 19.99", "19,99 TL", "19tl.99" ] function formPrice(value) { return parseFloat(value.replace(/[$€A-Za-z]/g, " ").trim().replace(/\\s/g, ".").replace(/[^0-9.,]/g, '').replace(/,/g, ".").replace(/\\.+/g, ".")) } function formPriceWithSymbol(value) { let symbol = value.replace(/[^$€A-Za-z]/g, "").toUpperCase() return symbol + value.replace(/[$€A-Za-z]/g, " ").trim().replace(/\\s/g, ".").replace(/[^0-9.,]/g, '').replace(/,/g, ".").replace(/\\.+/g, ".") } let prices = priceTexts.map((price) => { return formPrice(price) }) console.log(prices) prices = priceTexts.map((price) => { return formPriceWithSymbol(price) }) console.log(prices)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM