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.
>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);