Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 508
coffeemonitor Avatar asked Mar 31 '10 17:03

coffeemonitor


People also ask

How do I remove a character from a number string?

Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.

How do I get rid of non numeric characters in R?

[^[:alnum:]] is the parameter that removes the non-alphanumeric characters.


1 Answers

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

like image 111
Jacob Mattison Avatar answered Sep 21 '22 10:09

Jacob Mattison