Uppercase all words first char of a string in javascript
by Francis[ Edit ] 2014-09-05 11:11:18
Uppercase all words first char of a string in javascript
function toTitleCase(e)
{
var t=/^(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|vs?.?|via)$/i;
return e.replace(/([^W_]+[^s-]*) */g,function(e,n,r,i)
{
return r>0&&r+n.length!==i.length&&n.search(t)>-1&&i.charAt(r-2)!==":"&&i.charAt(r-1).search(/[^s-]/)<0?e.toLowerCase():n.substr(1).search(/[A-Z]|../)>-1?e:e.charAt(0).toUpperCase()+e.substr(1)
})
};
var str = "uppercase all words first char of a string in javascript";
var upStr = toTitleCase(str);
alert(upStr);
Result:
Uppercase All Words First Char of a String in Javascript