0

Lately iv'e been trying to find some ways to manipulate a string (for some project of mine) and i'm having a hard finding something that will mach my case. usually the string will include 3 numbers (can also be decimal - that's what make it more complicated) and separated by 1 / 2 signs ("-", "x", "*" and so on...) i did some research online and found this solution (which i thought it was good) .match(/\d+/g) when i tried it on some case the result was good var word = "9-6x3" word = word.match(/\d+/g) it gave me array with 3 indexes, each index held a number ['9', '6', '3'] (which is good), but if the string had a dot (decimal number) this regex would have ignored it. i need some regex which can ignore the dots in a string but can achieve the same result. case = var word = "9.5-9.3x7" output = ['9.5', '9.3', '7']

Àtishking
  • 263
  • 1
  • 4
  • 15

2 Answers2

0

Here is a simple regex that suits your requirement,

/\d+\.?\d*/g

Ramaraja
  • 2,526
  • 16
  • 21
0

Try this regular expression to allow for an optional decimal place:

word.match(/\d+([\.]\d+)?/g)

This says:

  • \d+ - any number of digits
  • ([\.]\d+)? - optionally one decimal point followed by digits
Always Learning
  • 5,510
  • 2
  • 17
  • 34