Skip to content

Instantly share code, notes, and snippets.

@GeekaholicLin
Created August 29, 2016 15:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeekaholicLin/5649247a03ad8f59f4eb18288d50251b to your computer and use it in GitHub Desktop.
Save GeekaholicLin/5649247a03ad8f59f4eb18288d50251b to your computer and use it in GitHub Desktop.
js 去除标点符号/空格且忽略大小写的回文串判定
function palindrome(str) {
  // Good luck!
  // \W -->匹配所有的字母、数字、下划线以外的字符
  str = str.replace(/\W|_/g,'').toLowerCase();
  var arrMaxIndex = str.length -1;
  for(var i = 0;i <=arrMaxIndex/2;i++){
    if(str.charAt(i)!==str.charAt(arrMaxIndex-i)){
      return false;
    }
  }
  return true;
}
@lin-xii
Copy link

lin-xii commented Dec 7, 2017

谢谢, 参考了下正则的部分

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment