[Vuejs]-Javascript โ€“ How to extract float number from a string for calculation using regex

1๐Ÿ‘

โœ…

Use this regex: /[0-9]+\,?[0-9]*/g

Then you can convert the , chars to .

// Example
const input = '2,5kg meal 500g cookie'
const floats = input
  .match(/[0-9]+\,?[0-9]*/g)
  .map(a => parseFloat(
    a.replace(',','.')
  )
)

// floats => [2.5, 500]

Leave a comment