I'm looking for some assistance in deciphering the functionality of my code. I'm trying to create a function that will take a string as input and eliminate all letters, leaving only numbers behind. The goal is to have this function return an array containing all the numbers extracted from the string. With help from stackoverflow, I've managed to come up with the following:
number = "32321FDFDS 44"
arr = number.replace(/[A-Za-z]/g," ").split(" ")
for(var i = arr.length - 1; i >= 0; i--){
if(arr[i] == "") {
arr.splice(i, 1);
}
}
As a result, I get
[ '32321', '44' ]
This output works well for me at the moment. However, I'm puzzled by how arr.splice(i,1)
manages to remove empty strings. It doesn't seem logical that it has this effect on the contents of arr
. Can someone provide clarification on this?