Files
30-seconds-of-code/snippets/isAnagram.md
30secondsofcode d0f1901ada Travis build: 1702
2018-02-19 13:57:10 +00:00

23 lines
682 B
Markdown

### isAnagram
Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters).
Use `String.toLowerCase()`, `String.replace()` with an appropriate regular expression to remove unnecessary characters, `String.split('')`, `Array.sort()` and `Array.join('')` on both strings to normalize them, then check if their normalized forms are equal.
```js
const isAnagram = (str1, str2) => {
const normalize = str =>
str
.toLowerCase()
.replace(/[^a-z0-9]/gi, '')
.split('')
.sort()
.join('');
return normalize(str1) === normalize(str2);
};
```
```js
isAnagram('iceman', 'cinema'); // true
```