Created
February 25, 2026 19:10
-
-
Save petergi/d4c9dfa16f199049fceb4c3f781aad0f to your computer and use it in GitHub Desktop.
Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters). - Use `String.prototype.toLowerCase()` and `String.prototype.replace()` with an appropriate regular expression to remove unnecessary characters.
- Use `String.prototype.split()`, `Array.prototype.sort()` and `Array.prototype…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters). | |
| - Use `String.prototype.toLowerCase()` and `String.prototype.replace()` with an appropriate regular expression to remove unnecessary characters. | |
| - Use `String.prototype.split()`, `Array.prototype.sort()` and `Array.prototype.join()` on both strings to normalize them, then check if their normalized forms are equal. | |
| const isAnagram = (str1, str2) => { | |
| const normalize = str => | |
| str | |
| .toLowerCase() | |
| .replace(/[^a-z0-9]/gi, '') | |
| .split('') | |
| .sort() | |
| .join(''); | |
| return normalize(str1) === normalize(str2); | |
| }; | |
| isAnagram('iceman', 'cinema'); // true | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment