Javascript Regular Expressions - Replace non-numeric characters

JavascriptRegex

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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.

Solution 3 - Javascript

Try this:

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

Solution 4 - Javascript

Try this:

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

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

Solution 5 - Javascript

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

Solution 6 - Javascript

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.

Solution 7 - Javascript

How about doing this:

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

Solution 8 - Javascript

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.

Solution 9 - Javascript

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

Solution 10 - Javascript

This one just worked for -ve to +ve numbers

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncoffeemonitorView Question on Stackoverflow
Solution 1 - JavascriptJacob MattisonView Answer on Stackoverflow
Solution 2 - JavascriptJeff BView Answer on Stackoverflow
Solution 3 - JavascriptLevi HackwithView Answer on Stackoverflow
Solution 4 - JavascriptEricView Answer on Stackoverflow
Solution 5 - JavascriptdkinzerView Answer on Stackoverflow
Solution 6 - Javascriptgm77View Answer on Stackoverflow
Solution 7 - JavascriptEricView Answer on Stackoverflow
Solution 8 - JavascriptPierreView Answer on Stackoverflow
Solution 9 - JavascriptgordonView Answer on Stackoverflow
Solution 10 - JavascriptMr. Adrien FeudjioView Answer on Stackoverflow