freeCodeCamp.org

Palindrome Checker

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 into the same case (lower or upper 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 2A3*3a2, 2A3 3a2, and 2_A3*3#A2.

palindrome("eye") should return a boolean.

palindrome("eye") should return true.

palindrome("_eye") should return true.

palindrome("race car") should return true.

palindrome("not a palindrome") should return false.

palindrome("A man, a plan, a canal. Panama") should return true.

palindrome("never odd or even") should return true.

palindrome("nope") should return false.

palindrome("almostomla") should return false.

palindrome("My age is 0, 0 si ega ym.") should return true.

palindrome("1 eye for of 1 eye.") should return false.

palindrome("0_0 (: /-\ :) 0-0") should return true.

palindrome("five|\_/|four") should return false.

问题

需要考虑边界情况

如何去除所有的标点符号???

  • \d matches any digit, equivalent to [0-9]
  • \D matches any character that’s not a digit, equivalent to [^0-9]
  • \w matches any alphanumeric character, equivalent to [A-Za-z0-9]
  • \W matches any non-alphanumeric character, equivalent to [^A-Za-z0-9]
  • \s matches any whitespace character: spaces, tabs, newlines and Unicode spaces
  • \S matches any character that’s not a whitespace
  • \0 matches null
  • \n matches a newline character
  • \t matches a tab character
  • \uXXXX matches a unicode character with code XXXX (requires the u flag)
  • . matches any character that is not a newline char (e.g. \n) (unless you use the s flag, explained later on)
  • [^] matches any character, including newline characters. It’s useful on multiline strings.

所有非字母\W(只要不是A-Za-z0-9)

但所有非字母\W包括下划线,所以还需要手动去除下划线

所有空格\s

注意要加上g标志

  1. function palindrome(str) {
  2. str = str.replace(/[\W_]/g, '').toLowerCase()
  3. console.log(str)
  4. let middle = Math.floor(str.length / 2)
  5. let left = str.slice(0, middle)
  6. let right = str.slice(-middle).split('').reverse().join("")
  7. if (left === right) {
  8. return true;
  9. } else {
  10. return false
  11. }
  12. }
  13. palindrome("not a palindrome")
  14. palindrome("_eye")
  15. palindrome("race car")
  16. palindrome("race car")
  17. palindrome("0_0 (: /-\ :) 0-0")

答案

freeCodeCamp Challenge Guide: Palindrome Checker

replace

JavaScript String.prototype.replace() - Replace Explained with Examples

直接字符串反转比较….

  1. function palindrome(str) {
  2. return (
  3. str.replace(/[\W_]/g, "").toLowerCase() ===
  4. str
  5. .replace(/[\W_]/g, "")
  6. .toLowerCase()
  7. .split("")
  8. .reverse()
  9. .join("")
  10. );
  11. }