单词首字母大写

// 普通low版
String.prototype.toJadenCase = function () { var arr = this.split(" "); var str = ""; for (var i = 0; i < arr.length; i++) { var cap = arr[i].charAt(0).toLocaleUpperCase(); arr[i] = arr[i].replace(/\w{1}/, cap); if (!str) { str += arr[i]; } else { str = str + " " + arr[i] } } return str; }; var str = "How can mirrors be real if our eyes aren't real"; console.log(str.toJadenCase())
// 正则高大上版本
String.prototype.toJadenCase = function () { return this.replace(/(\w)(\w+)/g,function(a,b,c){ return b.toUpperCase() + c; }) }; var str = "How can mirrors be real if our eyes aren't real"; console.log(str.toJadenCase())

 

posted @ 2017-07-26 21:43  无情码字员  阅读(262)  评论(0编辑  收藏  举报