Skip to content

Instantly share code, notes, and snippets.

@duan-li
Created December 5, 2013 06:14
Show Gist options
  • Save duan-li/7800878 to your computer and use it in GitHub Desktop.
Save duan-li/7800878 to your computer and use it in GitHub Desktop.
function ucwords (str) {
// http://kevin.vanzonneveld.net
// + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// + improved by: Waldo Malqui Silva
// + bugfixed by: Onno Marsman
// + improved by: Robin
// + input by: James (http://www.james-bell.co.uk/)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: ucwords('kevin van zonneveld');
// * returns 1: 'Kevin Van Zonneveld'
// * example 2: ucwords('HELLO WORLD');
// * returns 2: 'HELLO WORLD'
return (str + '').replace(/^([a-z\u00E0-\u00FC])|\s+([a-z\u00E0-\u00FC])/g, function ($1) {
return $1.toUpperCase();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment