user3157090
user3157090

Reputation: 537

javascript regular expression find decimal values

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

Answers (2)

Mike
Mike

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

Lalit Sachdeva
Lalit Sachdeva

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

Related Questions