Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove all but numerical values from a String in Javascript

I’m learning JS and want to know what’s the most efficient way to extract Integer values from Strings like below:

const String1 = '122,730 views'
const String2 = '2,990 likes'

Output:

122730
2990

Is there a JS function to remove all but numerical values from a String? In this case, parseInt() would just return 122 and 2.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

JavaScripts parseInt() function does not know any thousands separator.
You can just simply replace them beforehand.

const String1 = '122,730 views';
const Value1 = parseInt(String1.replace(/,/g, ""));

console.log(Value1);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading