Extract only numeric values in JavaScript

Posted by Rajnilari2015 under JavaScript category on | Points: 40 | Views : 1062
The below code will help to do so.

function test(){
//alert("6abc67*uv3$%^78*(op23".replace(/[\D]/g, ""));
//alert("6abc67*uv3$%^78*(op23".replace(/\D/g, ""));
alert("6abc67*uv3$%^78*(op23".replace(/[^{0-9}]/g, ""));
<input type="button" value="Test" onclick="test()" />

\D matches a character that is not a numerical digit and the non digit characters will be replaced by an empty string resulting in only the digits in a string.The g at the end of the regular expression literal is for "global" meaning that it replaces all matches. So all the 3 shown above will work

Comments or Responses

Posted by: Snaini on: 10/12/2015 Level:Starter | Status: [Member] | Points: 10
May be it will help to you to learn Regular Exp:

Login to post response