Returns true if the given string is a palindrome, and returns false instead.
If a string ignores punctuation, capitalization, and spaces, reads and reads exactly the same, then the string is Palindrome (palindrome).
Note that you need to remove the extra punctuation and whitespace from the string, and then convert the string to lowercase to verify that the string is a palindrome.
The value of the function argument can be "racecar", "racecar", and "race car".
Key code:
Remove the punctuation and whitespace from the string. You can use Str.replace () + regular expressions to match.
var newstr = str.replace (/[\ |\~| ' |\!| \@|\#|\$|\%|\^|\&|\*|\ (|\) |\-|\_|\+|\=|\| | \|\[|\]|\{|\}|\;|\:|\ "|\ ' |\,|\<|\.| \>|\/|\?] /g, "");
The complete code is as follows:
function Palindrome (str) {
//good luck!
var newstr = str.replace (/[\ |\~| ' |\!| \@|\#|\$|\%|\^|\&|\*|\ (|\) |\-|\_|\+|\=|\| | \|\[|\]|\{|\}|\;|\:|\ "|\ ' |\,|\<|\.| \>|\/|\?] /g, "");
var newnewstr = newstr.tolowercase ()//convert lowercase
var spl = Newnewstr.split ("");/split string array
var NEWSPL = Spl.reverse ( );/Flip
var newrel = Newspl.join ("");//Turn again to string
if (newnewstr = = Newrel) {//Compare return
true;
} else return false;
Palindrome ("Eye");
The above is a small set to introduce the use of regular expressions to determine whether a given character is a palindrome, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!