Extract all non-numeric values in JavaScript

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

function test(){
var input = "6abc67*uv3$%^78*(op23";
var output = input.replace(/[^\D]/g, "");
//alert("6abc67*uv3$%^78*(op23".replace(/[^\D]/g, ""));
//alert("6abc67*uv3$%^78*(op23".replace(/[{0-9}]/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.So if we negate that, we will get all non-numeric characters.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

Login to post response