2017-12-07 4 views
0

나는 실제로 간단한 문제가 있습니다. 다음 코드를 검토 할 때 의미하는 바를 이해하게 될 것입니다.toLowerCase 잘못된 하위 문자 "İ"

function isPalindrome(string){ 
    string = string.toLowerCase(); 
    var charArr = string.split(''); 

    charArr.forEach(char => { 
    console.log(char); 
    }) 
    console.log(charArr.length) 
} 

isPalindrome("İris"); 

출력

i 
̇ <- what is this ? 
r 
i 
s 
5 

당신이 보는대로 추가 문자를 추가. 나는이 문제를 해결할 수 없었다. 이 문제를 해결할 방법이 있습니까?

+2

당신이 ['toLocaleLowerCase()'] 시도 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ 적이 사용할 수 있습니다 문자열/toLocaleLowerCase)? –

+0

@FedericoklezCulloca 와우 작동. toLowerCase와의 차이점은 무엇입니까? – vulkan

+2

"İ"는 두 개의 유니 코드 문자로 구성되며 I 문자와 그 위에있는 점이 있습니다. '.toLocaleLowerCase()'는 그것을 하나의 문자로 인식합니다. – JJJ

답변

3

이것은 작동 중입니다. toLowerCase을 사용한 후에 가능한 특수 문자를 제거해야합니다. 당신은 replace(/[^\w\s]/gi, '')

Fiddle

function isPalindrome(str){ 
    var charArr = str.toLowerCase().replace(/[^\w\s]/gi, '').split(""); //toLowerCase --> remove special characters --> split the string 

    charArr.forEach(char => { 
    console.log(char); 
    }) 
    console.log(charArr.length) 
} 

isPalindrome("İris"); 
+0

더 쉬운 방법은 theLocaleLowerCase() – vulkan

+0

을 사용하는 것이 더 신뢰할 수있는 대답입니다. –

+0

'toLocaleLowerCase'는 모든 브라우저에서 작동하지 않습니다. 내 크롬 62.0.3202 (독일어)에서는 작동하지 않습니다. – alexP