JavaScript將首字母轉成大寫

fdpg 10年前發布 | 1K 次閱讀 JavaScript

/* To Title Case 1.1.1

String.prototype.toTitleCase = function() { return this.replace(/([\w&`'‘’"“.@:\/{([<>]+-? *)/g, function(match, p1, index, title) { if (index > 0 && title.charAt(index - 2) !== ":" && match.search(/^(a(nd?|s|t)?|b(ut|y)|en|for|i[fn]|o[fnr]|t(he|o)|vs?.?|via)[ -]/i) > -1) return match.toLowerCase(); if (title.substring(index - 1, index + 1).search(/['"{([]/) > -1) return match.charAt(0) + match.charAt(1).toUpperCase() + match.substr(2); if (match.substr(1).search(/[A-Z]+|&|[\w]+[._][\w]+/) > -1 || title.substring(index - 1, index + 1).search(/[])}]/) > -1) return match; return match.charAt(0).toUpperCase() + match.substr(1); }); };</pre>

 本文由用戶 fdpg 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!