Javascript regular expressions - replacing non-numeric characters

It works:

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

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

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

I am sure it is very simple, but my tests do not work.

+50
javascript regex
Mar 31 '10 at 17:23
source share
9 answers

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

+67
Mar 31 '10 at 17:26
source share

Replacing something that is not a number is a bit more complicated than replacing what is a number.

Those who propose simply adding a point ignore the fact that. also used as a period, therefore:

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

The specific regex in your problem will depend on the goal. If your line has only one number, you can do this:

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

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

+7
Mar. 31 '10 at 17:36
source share

Try the following:

 var.replace(/[^0-9\\.]+/g, ''); 
+4
Mar 31 '10 at 17:28
source share

Try the following:

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

This only matches decimal places (for example, "1", "1.0", ".5", but not "1.0.22")

+2
Mar 31 '10 at 17:29
source share

If you do not want to catch the IP address with decimal places:

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

Which will only catch numbers with one or zero period

+2
Mar 31 '10 at 17:33
source share

How to do it:

 var numbers = str.gsub(/[0-9]*\.?[0-9]+/, "#{0} "); 
0
Mar 31 '10 at 17:48
source share

Weak and short inline replacement for non-digital characters in an ASP.Net text field:

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

Change the regex part as you needed. Many and many people complain that the cursor goes straight to the end when using the arrow keys, but people tend to cope with this without noticing it, for example, an arrow ... arrow ... arrow ... okay, then .. backspace back space, enter new characters.

0
Feb 15 '13 at
source share

there are many correct answers, just indicating that you may need to consider negative signs as well. "\-" add this to any existing answer to allow negative numbers.

0
Mar 15 '16 at 13:28
source share

Here are a few types of jQuery input classes that 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 you to use only numeric ones - like other solutions.

nmbr allows only positive / negative decimal places. Negative should be the first character (you can add a “+” to the filter if you need it) strips -3.6.23.333 to -3.623333

I put nmbr because I'm tired of looking for a way to keep only 1 decimal and negative in 1st position

0
Aug 23 '16 at 15:55
source share



All Articles