Reputation: 537
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
Upvotes: 0
Views: 56
Reputation: 349
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.
Upvotes: 1
Reputation: 6619
this is the regex please have a look
[0-9]*\.[0-9]*
or this is the refined one
[0-9]+\.[0-9]+
Upvotes: 1