Javascript String ucwords()

Description

Javascript String ucwords()


String.prototype.ucwords = function() {
    return (this + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
      return $1.toUpperCase();
  });// w w  w.j  a  va  2 s  .c om
}

Javascript String ucwords()

String.prototype.ucwords = function () {
 var words = this.split(' ');
 for (var i = 0; i < words.length; i++)
  words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
 return words.join(' ');
};

Javascript String ucwords()

String.prototype.ucwords = function() {
 var str = this.toLowerCase();
 
 return str.replace(/(^([a-zA-Z]))|(\b([a-zA-Z]))/g, function($1) {
  return $1.toUpperCase();
 });/* w ww  .ja va2s .  c om*/
};

Javascript String ucwords()

/**/*from ww w  .j  ava 2s .c  om*/
@Name: String.prototype.ucwords
@Author: Paul Visco
@Version: 1.0 11/19/07
@Description: Converts all first letters of words in a string to uppercase.  Great for titles.
@Return: String The original string with all first letters of words converted to uppercase.
@Example:
var myString = 'hello world';

var newString = myString.ucwords();
//newString = 'Hello World'
*/

String.prototype.ucwords = function(){
 var arr = this.split(' ');
 
 var str ='';
 arr.forEach(function(v){
  str += v.charAt(0).toUpperCase()+v.slice(1,v.length)+' ';
 });
 return str;
};



PreviousNext

Related