javascript - Check for Palindrome (ignore special characters, whitespace, and capitalization) -
objective determine whether or not string palindrome ignoring whitespaces, special characters, , capitalization.
javascript
function palindrome(str) { //remove punctuation, whitespace, capitalization, , special characters original string - var original = str.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"") .replace(/\s/g," ").tolowercase(); //take original sentence , reverse var reverse = original.split('').reverse().join(''); //compare original vs reversed if (original == reverse) { return true; } else { return false; } } palindrome("eye");
questions
- i've created aforementioned code checking online (palindrome check). missing anything?
- i using regex handle punctuation , whitespace , checks out.
you can join 2 replace chained methods 1 including \s
character class (and add missing common special characters):
var original = str.replace(/[\s"'.,-\/#!$%\^&*;:{}=\-_`~()\\\[\]@+|?><]/g,"").tolowercase();
Comments
Post a Comment