I'm new to stackoverflow and would appreciate some advice to help me solve this problem. I have two strings that I want to extract numbers from
String 1 = "12.3,Name1,3,4,Name2,35,Name3"
returns [12.3,3,4,35]
Which is the required result.
String 2 = "12.3,22,Q"
returns [12.322]
Which is the incorrect result, It should be [12.3,22]
I have commented on my code with the steps I have taken to complete the task. Again many thanks for your advice and help in advance.
Here is a copy of my code:
function extractNumbers(str) {
//Use replace() to replace all instances of letters/alpha*
let onlyNumbersString = str.replace(/[a-z]/ig, '');
//remove "," using replace()*
onlyNumbersString = onlyNumbersString.replace(",", "");
//Create an array of numbers*
let arrayOfNumbers = onlyNumbersString.split(',');
let result = arrayOfNumbers.map((x) => parseFloat(x))
console.log(arrayOfNumbers)
console.log(result);
const filtered = result.filter(function(e) {
return e
})
console.log(filtered)
}
let numbers = "12.3,Name1,3,4,Name2,35,Name3" //returns [12.3,3,4,35]
//"12.3,22,Q" returns [12.322]*
extractNumbers(numbers)