Strip all non-numeric characters from string in JavaScript -
Consider a non-dom scenario where you want to remove all non-numeric characters from the string using javascript / ECMAScript . Any characters that are in the range of 0-9 should be kept.
var myString = 'abc123.8 & lt; Blah & gt; '; // The desired output is 1238
How do you get it in plain JavaScript? Please remember this is a non-DOM scenario, so jQuery and other solutions that include browser and keypress events are not appropriate. Use the string method with a regex of
\ D
Which is a miniature character square that matches all non-digits:
myString = myString.replace (/ \ D / g, '');
Comments
Post a Comment