How to remove all non-alphabet characters, javascript

Viewed 3363

I am asked to check if a string is a Palindrome. To not be case sensitive. To ignore all characters that are not letters.

My Answer

 function palindrome(str) {
  var oldStr = str.toLowerCase().replace(/\s+|\,|\.|\_|\-|\:|\(|\)|\/|\\/g,  '');

  var newStr = str.replace(/\s+|\,|\.|\_|\-|\:|\(|\)|\/|\\/g, '').split("").reverse().join("").toLowerCase();

  if ( oldStr === newStr){
    return true;
  }
  else {
    return false;
  }
}
palindrome("ininiNI");

The function is to be checked with any string possibility. Example: ("0_0 (: /-\ :) 0-0") Which according to the requirements should return true.

I could not find a better solution in JavaScript then the one above.

Is there a faster/better way than just writing out each possible character to be removed/replaced? (especially since what I wrote is far from exhaustive...)

1 Answers
Related