Get Only Numbers from string in JavaScript



Some time developers need a code to extract number from string that contains alphabets, special characters. Below is a JavaScript function that does the job for you.

Code simply extracts number if found else returns empty string.

JavaScript RegExp \d Metacharacter  is used to find a digit from 0-9.
In above function you can replace /\d+/g  with /[0-9]+/g

both will generate same result. The only difference is \d is a short name of [0-9]

Hi! I am Sartaj Husain. I am a Professional Software Developer, live in Delhi. I write blogs in my free time. I love to learn and share the knowledge with others because it is no good to try to stop knowledge from going forward. So free posts and tutorials. more..

No comments