93

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.

1
  • Are you looking for a single number, or multiple numbers? I.e. for "12.3 a b5" do you want to return "12.3 5" or "12.35", or is that input string not even possible? Commented Mar 31, 2010 at 17:54

12 Answers 12

135

Simply: var.replace(/[^\d.-]+/g, '');

Sign up to request clarification or add additional context in comments.

1 Comment

escaping the period is unnecessary, I believe.
10

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.

Comments

7

Try this:

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

Comments

3

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.

Comments

2

Try this:

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

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

1 Comment

Actually, this matches B.006, or blah123. He is looking for the negative case.
1

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

2 Comments

This is wrong. This matches non-numbers, followed by a possible period, followed by numbers. The poster wants to replace all NON-numbers with nothing.
Though I think that this says match anything that starts with one or more numbers, followed by 0 or 1 period followed by 0 or more numbers.
0

How about doing this:

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

Comments

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.

Comments

0

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

Comments

0

This one just worked for -ve to +ve numbers

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

Comments

0

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

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

Comments

0

Numbers:

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

Integer (unsigned):

var.replace(/[^\d]+/g, '');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.