2016-09-09 4 views
0

하드 코딩 된 일부 사례를 제외하고 문자열의 모든 단어를 어떻게 대문자로 표시합니까? 예를 들어 : 단어를 제외 할 "드", "라" 입력 : "드가-URB.JARDÍN DE LAS AVENIDAS" 출력 : "드가-Urb.Jardín 드 라스 Avenidas는"모든 단어를 대문자로 변환

답변

1

이 기능을 사용할 수 있습니다 각 단어에 :

function toTitleCase(str) 
{ 
    // Protected words that you don't want to Titlecase 
    var dontChangeTheseWords = ["de", "la"]; 
    if(dontChangeTheseWords.indexOf(str) > -1) 
    { 
     return str; 
    } 

    return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); 
} 
+0

"Degas-urb.jardín De Las Avenidas"를 반환합니다. 결과는 "Degas-Urb.Jardín de las Avenidas"여야합니다. –

+0

단어 경계 대신 공백을 사용 했으므로 –

관련 문제