0

I have requirement to get the decimal values from string if it exists otherwise null, how can do that in javascript, the below are possibilities values i want to get the decimal value.

6Pay-30D 3Pay-30D 19.95-1st Pay Amount 3Pay-30D SH3Pay

user3157090
  • 517
  • 3
  • 12
  • 29
  • here is a similar [question](http://stackoverflow.com/questions/10411833/how-to-extract-decimal-number-from-string-using-javascript) – svnm Dec 16 '14 at 09:07

2 Answers2

1

this is the regex please have a look

[0-9]*\.[0-9]*

or this is the refined one

[0-9]+\.[0-9]+
Lalit Sachdeva
  • 6,469
  • 2
  • 19
  • 25
1

actually, I don't know if you want all the numbers out, you can try like this:

var str = "6Pay-30D 3Pay-30D 19.95-1st Pay Amount 3Pay-30D SH3Pay";
str.match(/\d+(\.\d+)?/g);

below is the output:

['6','30','3','30','19.95','1','3','30','3']

if you just want "19.95" in the input. you'll try like this:

str.match(/\d+(\.\d+){1}/g);

below is the output:

[ '19.95' ]

finally, if you just want first matched result, just remove the 'g'. hope it helpful.

Mike
  • 349
  • 1
  • 9