[JS]检查回文字符串
题目描述:如果一个字符串忽略标点符号、大小写和空格,正着读和反着读一模一样,那么这个字符串就是palindrome(回文)。
题目来源:freecodecamp
重点:除去所有标点符号
1 str=str.replace(/[\ |\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\-|\_|\+|\=|\||\\|\[|\]|\{|\}|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?]/g,"");
完整代码:
1 function palindrome(str) { 2 // 请把你的代码写在这里 3 str=str.toLowerCase(); 4 str=str.replace(/\s/g,""); 5 str=str.replace(/[\ |\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\-|\_|\+|\=|\||\\|\[|\]|\{|\}|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?]/g,""); 6 var nStr; 7 var array=str.split(""); 8 array.reverse(); 9 nStr=array.join(""); 10 if(nStr===str) 11 return true; 12 else return false; 13 } 14 15 16 17 palindrome("A man, a plan, a canal. Panama");
补充参考:
正则表达式使用方法:https://blog.csdn.net/zqjflash/article/details/25557567
正则表达式:https://www.cnblogs.com/Egbertbaron/p/7218373.html
@jm_epiphany
    原创供学习参考使用,转载请注明出处http://www.cnblogs.com/cuphoria/
@jm_epiphany
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号