美文网首页
regex find palindrome

regex find palindrome

作者: 成江 | 来源:发表于2018-03-03 23:59 被阅读8次

Return true if the given string is a palindrome. Otherwise, return false.

A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.

Note

You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything lower case in order to check for palindromes.

We'll pass strings with varying formats, such as "racecar", "RaceCar", and "race CAR" among others.

We'll also pass strings with special symbols, such as "2A33a2", "2A3 3a2", and "2_A33#A2".

function palindrome(str) {
  // Good luck!
  str = str.replace(/[^a-zA-Z0-9]+/g, "");
  console.log(str);
  str = str.toLowerCase();
  for (var i = 0; i < str.length; i++) {
    if (str[i] !== str[str.length - 1 - i]) {
      return false;
    }
  }
  return true;
}

相关文章

网友评论

      本文标题:regex find palindrome

      本文链接:https://www.haomeiwen.com/subject/tvaxfftx.html