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

  1. i've created aforementioned code checking online (palindrome check). missing anything?
  2. 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

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -