1function palindrome(str) {
2 // Step 1. Lowercase the string and use the RegExp to remove unwanted characters from it
3 var re = /[\W_]/g; // or var re = /[^A-Za-z0-9]/g;
4
5 var lowRegStr = str.toLowerCase().replace(re, '');
6 // str.toLowerCase() = "A man, a plan, a canal. Panama".toLowerCase() = "a man, a plan, a canal. panama"
7 // str.replace(/[\W_]/g, '') = "a man, a plan, a canal. panama".replace(/[\W_]/g, '') = "amanaplanacanalpanama"
8 // var lowRegStr = "amanaplanacanalpanama";
9
10 // Step 2. Use the same chaining methods with built-in functions from the previous article 'Three Ways to Reverse a String in JavaScript'
11 var reverseStr = lowRegStr.split('').reverse().join('');
12 // lowRegStr.split('') = "amanaplanacanalpanama".split('') = ["a", "m", "a", "n", "a", "p", "l", "a", "n", "a", "c", "a", "n", "a", "l", "p", "a", "n", "a", "m", "a"]
13 // ["a", "m", "a", "n", "a", "p", "l", "a", "n", "a", "c", "a", "n", "a", "l", "p", "a", "n", "a", "m", "a"].reverse() = ["a", "m", "a", "n", "a", "p", "l", "a", "n", "a", "c", "a", "n", "a", "l", "p", "a", "n", "a", "m", "a"]
14 // ["a", "m", "a", "n", "a", "p", "l", "a", "n", "a", "c", "a", "n", "a", "l", "p", "a", "n", "a", "m", "a"].join('') = "amanaplanacanalpanama"
15 // So, "amanaplanacanalpanama".split('').reverse().join('') = "amanaplanacanalpanama";
16 // And, var reverseStr = "amanaplanacanalpanama";
17
18 // Step 3. Check if reverseStr is strictly equals to lowRegStr and return a Boolean
19 return reverseStr === lowRegStr; // "amanaplanacanalpanama" === "amanaplanacanalpanama"? => true
20}
21
22palindrome("A man, a plan, a canal. Panama");