Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 06-10-2009, 01:55 AM   PM User | #1
Squishinator
New Coder

 
Join Date: Jun 2009
Posts: 20
Thanks: 3
Thanked 0 Times in 0 Posts
Squishinator is an unknown quantity at this point
Capitalizing first letter of word

Is this possible in javascript? How do you do it?
Squishinator is offline   Reply With Quote
Old 06-10-2009, 03:41 AM   PM User | #2
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 386
Thanks: 10
Thanked 56 Times in 54 Posts
mrhoo is on a distinguished road
/* You oughta be able to cut it out of this function-
it capitalizes a string, or each word in a string.
*/
Code:
String.prototype.capit= function(deep){
	var str= this.toLowerCase();
	var Rx= deep? /\b([a-z']+)\b/g: /\b([a-z]+)\b/;
	str= str.replace(Rx, function(w){
		return w.charAt(0).toUpperCase()+ w.substring(1);
	});
	return str;
}
var s="these are the times that try men's souls."
alert(s.capit()+'\n'+s.capit(true))

Last edited by mrhoo; 06-10-2009 at 03:46 PM..
mrhoo is offline   Reply With Quote
Old 06-10-2009, 09:26 AM   PM User | #3
Philip M
Master Coder

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 7,670
Thanks: 94
Thanked 917 Times in 898 Posts
Philip M will become famous soon enoughPhilip M will become famous soon enough
Another (slightly simpler) method or variation:-


Code:
var str = "jEAn-paul o'flaNAGan-macDONald is a good guy."
str = str.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
alert(str);

Looking for a pen? Look no further than Pen Island: www.penisland.net
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:05 PM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.