I want to check if my input string contains numbers and display an array of these numbers. The number is composed of an optional sign (-
or +
), one or more consecutive digits and an optional fractional part. The fractional part is composed of a dot .
followed by zero or more digits.
For example f2('a1 12 13.b -14.5+2')
: returns [1, 12, 13, -14.5, 2]
I try this code from a response here
function f2(input) {
let str = String(input);
for (let i = 0; i < str.length; i++) {
console.log(str.charAt(i));
if (!isNaN(str.charAt(i))) {
//if the string is a number, do the following
return str.charAt(i);
}
}
}
let result = f2("a1 12 13.b -14.5+2");
console.log(result);