Javascript Regular Expressions convert String to int
function filterInt(value) { if (/^[-+]?(\d+|Infinity)$/.test(value)) { return Number(value) } else {//from w w w. j ava 2 s.c o m return NaN } } console.log(filterInt('42')); console.log(filterInt('-42')); console.log(filterInt('+42')); console.log(filterInt('Infinity')); console.log(filterInt('1.618')); console.log(filterInt('1.ASDF')); console.log(filterInt('ASDF.618')); console.log(filterInt('1E10')); console.log(filterInt('1n'));