I am trying to loop over an array which contains strings that I want my input string to compare with. So approach is I am looping over an input string to check if each character matches with one of the elements present in the array. If not just replace that character with just ''. Note: regular expression is really not an option.
Here is how my JavaScript looks like
var input = 'this is A [{}].-_+~`:; *6^123@#$%&*()?{}|\ ';
input.toLowerCase(input)
var allowed = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d', 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','s','à','â','ä','è','é','ê','ë','î','ï','ô','œ','ù','û','ü','ÿ','ç','À','Â','Ä','È','É','Ê','Ë','Î','Ï','Ô','Œ','Ù','Û','Ü','Ÿ','Ç', ' ']
var cleanStr = '';
for(var i = 0; i < input.length; i++){
for(var j = 0; j< allowed.length; j++){
if(input[i] !== allowed[j]){
cleanStr = input.replace(input[i], ' ');
console.log(cleanStr);
}
}
}
The console log output doesn't appear to be any different than the input field. What am I missing?
Here is my fiddle
O(n^2)loop to achieve this when a pre-compiled regex of allowable characters would be vastly more efficientinput = input.toLowerCase()2. You'vestwice in the array.