Javascript Regular Expressions - Replace non-numeric characters

This works:

var.replace(/[^0-9]+/g, '');

That simple snippet will replace anything that is not a number with nothing.

But decimals are real too. So, I'm trying to figure out how to include a period.

I'm sure it's really simple, but my tests aren't working.

125828 次浏览

Did you escape the period? var.replace(/[^0-9\.]+/g, '');

Try this:

var.replace(/[^0-9\\.]+/g, '');

Try this:

var.replace(/[0-9]*\.?[0-9]+/g, '');

That only matches valid decimals (eg "1", "1.0", ".5", but not "1.0.22")

If you don't want to catch IP address along with decimals:

var.replace(/[^0-9]+\\.?[0-9]*/g, '');

Which will only catch numerals with one or zero periods

Replacing something that is not a number is a little trickier than replacing something that is a number.

Those suggesting to simply add the dot, are ignoring the fact that . is also used as a period, so:

This is a test. 0.9, 1, 2, 3 will become .0.9123.

The specific regex in your problem will depend a lot on the purpose. If you only have a single number in your string, you could do this:

var.replace(/.*?(([0-9]*\.)?[0-9]+).*/g, "$1")

This finds the first number, and replaces the entire string with the matched number.

How about doing this:

var numbers = str.gsub(/[0-9]*\.?[0-9]+/, "#{0} ");

Sweet and short inline replacing of non-numerical characters in the ASP.Net Textbox:

 <asp:TextBox ID="txtJobNo" runat="server" class="TextBoxStyle" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" />

Alter the regex part as you'ld like. Lots and lots of people complain about the cursor going straight to the end when using the arrow keys, but people tend to deal with this without noticing it for instance, arrow... arrow... arrow... okay then... backspace back space, enter the new chars.

there's a lot of correct answers already, just pointing out that you might need to account for negative signs too.. "\-" add that to any existing answer to allow for negative numbers.

Here are a couple of jQuery input class types I use:

$("input.intgr").keyup(function (e) { // Filter non-digits from input value.
if (/\D/g.test($(this).val())) $(this).val($(this).val().replace(/\D/g, ''));
});
$("input.nmbr").keyup(function (e) { // Filter non-numeric from input value.
var tVal=$(this).val();
if (tVal!="" && isNaN(tVal)){
tVal=(tVal.substr(0,1).replace(/[^0-9\.\-]/, '')+tVal.substr(1).replace(/[^0-9\.]/, ''));
var raVal=tVal.split(".")
if(raVal.length>2)
tVal=raVal[0]+"."+raVal.slice(1).join("");
$(this).val(tVal);
}
});

intgr allows only numeric - like other solutions here.

nmbr allows only positive/negative decimal. Negative must be the first character (you can add "+" to the filter if you need it), strips -3.6.23.333 to -3.623333

I'm putting nmbr up because I got tired of trying to find the way to keep only 1 decimal and negative in 1st position

This one just worked for -ve to +ve numbers

<input type="text" oninput="this.value = this.value.replace(/[^0-9\-]+/g, '').replace(/(\..*)\./g, '$1');">

I use this expression to exclude all non-numeric characters + keep negative numbers with minus sign.

variable.replace(/[^0-9.,\-]/g,'')