분류
javascript
Javascript 30초 Snippet - String : isAnagram
본문
문자열이 다른 문자열의 아나그램인지 확인합니다 (대소 문자 구분, 공백, 문장 부호 및 특수 문자 무시).
String.toLowerCase(), String.prototype.replace()를 적절한 정규식과 함께 사용하여 불필요한 문자를 제거하십시오. String.prototype.split(''), Array.prototype.sort() 및 Array.prototype.join('')를 사용하여 두 문자열을 정규화 한 다음 정규화 된 형식이 같은지 확인하십시오.
const isAnagram = (str1, str2) => { const normalize = str => str .toLowerCase() .replace(/[^a-z0-9]/gi, '') .split('') .sort() .join(''); return normalize(str1) === normalize(str2); };
ex)
isAnagram('iceman', 'cinema'); // true
- 이전글Javascript 30초 Snippet - String : isLowerCase 19.11.25
- 다음글Javascript 30초 Snippet - String : isAbsoluteURL 19.11.25