## The Challenge {#thechallenge}

。如果`str`

，否则返回`false`

## What Is a Palindrome? {#whatisapalindrome}

• Eye
• Racecar
• A Man, A Plan, A Canal -- Panama!

## Step 1 - Equalize All Casing {#step1equalizeallcasing}

``````function palindrome(str) {
// 1) Lowercase the input
}

palindrome("eye");
``````

## Step 2 - Strip Non-Alphanumeric Characters {#step2stripnonalphanumericcharacters}

``````"A Man, A Plan, A Canal -- Panama!"

// lowercase everything
// strip out non-alphanumeric characters

"amanaplanacanalpanama"
``````

### What does alphanumeric mean? {#whatdoesalphanumericmean}

``````function palindrome(str) {
// 1) Lowercase the input
// 2) Strip out non-alphanumeric characters
}

palindrome("eye");
``````

## Step 3 - Compare String to Its Reverse {#step3comparestringtoitsreverse}

``````return string === reversedString
``````

）用于JavaScript中的比较。如果两个字符串相同，则它是回文并且我们返回`true`
！如果不是，我们回来`false`

``````function palindrome(str) {
// 1) Lowercase the input
// 2) Strip out non-alphanumeric characters
// 3) return string === reversedString
}

palindrome("eye");
``````

## Executing Step 1 - Lowercase {#executingstep1lowercase}

``````function palindrome(str) {
// 1) Lowercase the input
const alphanumericOnly = str.toLowerCase();

// 2) Strip out non-alphanumeric characters
// 3) return string === reversedString
}

palindrome("eye");
``````

## Executing Step 2 - Alphanumeric Only {#executingstep2alphanumericonly}

### The .match method {#thematchmethod}

。它需要一个参数来指示您希望在给定字符串中查找哪些字符。

``````myName = 'yazeed';

myName.match('e');
// ["e", index: 3, input: "yazeed", groups: undefined]
``````

。那是我们在字符串中找到的匹配`'yazeed'`

### Regular Expressions (Regex) {#regularexpressionsregex}

`.match`

``````myName = 'yazeed';

myName.match(/e/);
// ["e", index: 3, input: "yazeed", groups: undefined]
``````

``````myName = 'yazeed';

myName.match(/e/g);
// ^^ Notice the little g now ^^
// ["e", "e"]
``````

``````myName.match(/a/g);
// ["a"]

myName.match(/z/g);
// ["z"]
``````

### Finding all alphanumeric characters {#findingallalphanumericcharacters}

``````/[a-z0-9]/g
``````

。这个正则表达式可以分为3个部分。

1. A character set `[]` - match any character between these brackets.
2. `a-z` - match all lowercase letters
3. `0-9` - match all numbers

``````myName = 'yazeed';

myName.match(/[a-z0-9]/g);
// ["y", "a", "z", "e", "e", "d"]
``````

``````crazyInput = '0_0 (: /-\ :) 0-0';

crazyInput.match(/[a-z0-9]/g);
// ["0", "0", "0", "0"]
``````

``````function palindrome(str) {
const alphanumericOnly = str
// 1) Lowercase the input
.toLowerCase()
// 2) Strip out non-alphanumeric characters
.match(/[a-z0-9]/g);

// 3) return string === reversedString
}

palindrome("eye");
``````

## Executing Step 3 - Compare String to Its Reverse {#executingstep3comparestringtoitsreverse}

### Array.reverse {#arrayreverse}

`reverse`

``````[1, 2, 3].reverse();
// [3, 2, 1]
``````

### Array.join {#arrayjoin}

`join`

``````[1, 2, 3].join();
// "1,2,3"
``````

``````[1, 2, 3].join('my separator');
// "1my separator2my separator3"

[1, 2, 3].join(',');
// "1,2,3"

[1, 2, 3].join(', ');
// "1, 2, 3"

[1, 2, 3].join('sandwich');
// "1sandwich2sandwich3"
``````

``````'Ra_Ce_Ca_r   -_-'
.toLowerCase()
.match(/[a-z0-9]/g)
.join('');

// "racecar"
``````

``````'Ra_Ce_Ca_r   -_-'
.toLowerCase()
.match(/[a-z0-9]/g)
// flip it around
.reverse()
.join('');

// "racecar"
``````

``````'yazeed'
.toLowerCase()
.match(/[a-z0-9]/g)
// flip it around
.reverse()
.join('');

// "deezay"
``````

## The Final Code {#thefinalcode}

``````function palindrome(str) {
const alphanumericOnly = str
// 1) Lowercase the input
.toLowerCase()
// 2) Strip out non-alphanumeric characters
.match(/[a-z0-9]/g);

// 3) return string === reversedString
return alphanumericOnly.join('') ===
alphanumericOnly.reverse().join('');
}

palindrome("eye");
``````

## Summary {#summary}

1. Lowercase input via `str.toLowerCase()`;
2. Match all alphanumeric characters using a regular expression via `str.match(/[a-z0-9]/g)`.
3. Use `Array.reverse` and `Array.join` on the alphanumeric matches to compare the original against its reversed self. If they're identical we get back `true`, otherwise we get back `false`!

（未经同意，请勿转载）