jQuery删除所有字符,但数字和小数 [英] jQuery remove all characters but numbers and decimals
本文介绍了jQuery删除所有字符,但数字和小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var price = "$23.03";
var newPrice = price.replace('$', '')
这是有效的,但价格也可以是:
This works, but price can also be such as:
var price = "23.03 euros";
以及许多其他货币。
无论如何我只能留下数字和小数(。)?
Is there anyway that I could leave only numbers and decimal(.)?
推荐答案
var newPrice = price.replace(/[^0-9\.]/g, '');
不需要jQuery。您还需要检查是否只有一个小数点,如下所示:
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.
}
这篇关于jQuery删除所有字符,但数字和小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文