snippetjavascriptTip
Check if a JavaScript string is an anagram of another string
Viewed 0 times
javascriptcheckanagramstringanother
Problem
An anagram of a string is another string that contains the same characters, only in a different order. It's common to also ignore case, spaces, punctuation, and special characters when checking for anagrams.
The simplest way to compare two strings for anagrams is to normalize them by removing unnecessary characters and sorting them. This way, you can easily compare the normalized strings to see if they are equal.
Implementing this in JavaScript means using
The simplest way to compare two strings for anagrams is to normalize them by removing unnecessary characters and sorting them. This way, you can easily compare the normalized strings to see if they are equal.
Implementing this in JavaScript means using
String.prototype.toLowerCase() and String.prototype.replace() with an appropriate regular expression to remove unnecessary characters. Then, you can use String.prototype.split(), Array.prototype.sort(), and Array.prototype.join() to normalize both strings and check if their normalized forms are equal.Solution
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'); // trueImplementing this in JavaScript means using
String.prototype.toLowerCase() and String.prototype.replace() with an appropriate regular expression to remove unnecessary characters. Then, you can use String.prototype.split(), Array.prototype.sort(), and Array.prototype.join() to normalize both strings and check if their normalized forms are equal.Code Snippets
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'); // trueContext
From 30-seconds-of-code: is-anagram
Revisions (0)
No revisions yet.