Extract only numeric values in JavaScript

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

<html>
<head>
<script>
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, ""));
}
</script>
</head>
<body>
<input type="button" value="Test" onclick="test()" />
</body>
</html>


\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:
http://www.w3schools.com/jsref/jsref_obj_regexp.asp

Login to post response