2017-07-15 53 views
0

我有以下字符串:如何使所有大寫在JavaScript除非字符串包含'

A new way's to get home 

輸出需要是:

A New Way's To Get Home 

我嘗試使用此代碼:

var string = "A new way's to get home"; 

string = string.replace(/\(\d+\)/g, '').trim().replace(': ', ' - ').replace(/\b[a-z]/g,function(f){return f.toUpperCase();}); 

alert(string); 

但是我得到這個:

A New Way'S To Get Home 

我需要得到所有字符串在開始到大寫,但在這裏問題是方式的,因爲它包含'和上面這個正則表達式需要返回'不是'我如何我需要重寫這個正則表達式來返回正確的值?

+0

的[字符串轉換爲標題情況下使用JavaScript]可能的複製(https://stackoverflow.com/questions/196972/convert-string -to-title-case-with-javascript) – Daniel

回答

1

Convert string to title case with JavaScript可能是你在找什麼。這就是所謂的標題案例。功能粘貼下面的易用性:

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

信貸格雷格院長

+0

感謝...作品像魅力 – John

相關問題