How to extract decimal number from string using JavaScript

ank87 picture ank87 · May 2, 2012 · Viewed 17.3k times · Source

I want to extract decimal number 265.12 or 0.0 from alphanumeric string (say amount) having value $265.12+ or $265.12- or $0.0+ or $0.0- and use it apply struts logic tag in jsp. Not sure how to extract number maybe with help of JavaScript.

Answer

Adil picture Adil · May 2, 2012

You can use regex like this,

Live Demo

var amount = "$265.12+";
var doublenumber = Number(amount.replace(/[^0-9\.]+/g,""));