6
var price = "$23.03";
var newPrice = price.replace('$', '')

This works, but price can also be such as:

var price = "23.03 euros";

and many many other currencies.

Is there anyway that I could leave only numbers and decimal(.)?

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
user2179950
  • 93
  • 1
  • 1
  • 3

2 Answers2

33
var newPrice = price.replace(/[^0-9\.]/g, '');

No jQuery needed. You will also need to check if there is only one decimal point though, like this:

var decimalPoints = newPrice.match(/\./g);

// Annoyingly you have to check for null before trying to
// count the number of matches.
if (decimalPoints && decimalPoints.length > 1) {
    // do whatever you do when input is invalid.
}
Matt Cain
  • 5,638
  • 3
  • 36
  • 45
2
var newprice = price.replace( /\D+$/, '');
John Dvorak
  • 26,799
  • 13
  • 69
  • 83