JavaScript将首字母转成大写详解编程语言

/* To Title Case 1.1.1 
 * David Gouch <http://individed.com> 
 * 23 May 2008 
 * License: http://individed.com/code/to-title-case/license.txt 
 * 
 * In response to John Gruber's call for a Javascript version of his script:  
 * http://daringfireball.net/2008/05/title_case 
 */ 
 
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); 
    }); 
};

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/8862.html

(0)
上一篇 2021年7月18日
下一篇 2021年7月18日

相关推荐

发表回复

登录后才能评论