Skip to content

Instantly share code, notes, and snippets.

@Natumsol
Created April 14, 2017 01:27
Show Gist options
  • Select an option

  • Save Natumsol/dcdd4b614bb969c679b97c0221018838 to your computer and use it in GitHub Desktop.

Select an option

Save Natumsol/dcdd4b614bb969c679b97c0221018838 to your computer and use it in GitHub Desktop.
将句子中单词首字母大写
function upperFirstChar(str) {
// replace的第二个参数可以为函数,第一个为匹配到的字符串,第二个为匹配在原字符串中的索引,第三个为原字符串
return str.replace(/\w+/g, function(match, offset, string){
return match[0].toUpperCase() + match.slice(1);
});
}
console.log(upperFirstChar(" hello, world my World hehe"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment