I am trying to solve a palindrome problem. The palindrome can contain letters and numbers, but everything else must be removed. My code below is not returning the correct result.
function palindrome(str) {
const regex = /[\s]|[[:punct:]]/gi;
// const regex = /age/gi;
const string = str.replace(regex, "").toLowerCase().split("");
console.log(string);
let aPointer = 0;
let bPointer = string.length - 1;
while (aPointer <= bPointer) {
if (string[aPointer] !== string[bPointer]) {
return false;
}
aPointer += 1;
bPointer -= 1;
}
return true;
}
console.log(palindrome("My age is 0, 0 si ega ym."));
The output removes the spaces but not the punctuation. Am I getting the syntax wrong?