473,738 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function to capitalise each word

To save me re-inventing the wheel, does anyone have a function to
capitalise each word in form data? I was thinking along the lines of
capitalise the first letter of the string, then any other letter after a
space or hyphen.

So many users seem to think that just because they are online they can
use names like "mr john smith" and so I have to manually change them.
--
Nige

Please replace YYYY with the current year
ille quis mortem cum maximus ludos, vincat
Jul 20 '05
13 6383
Fox wrote on 11 nov 2003 in comp.lang.javas cript:
String.prototyp e.ucwords = function(){
return this.replace(/\b\w/gi, function(c,i,s) { return
c.toUpperCase() ; });
}


String.prototyp e.ucwords = function(){
return this.toLowerCas e().replace(/\b\S/g, function(x) { return
x.toUpperCase() ; });
}

The rest has to be lowercase, IMHO.

I do not think the i in /gi is necessary.

Why replace the whole word?

Please explain for us mortals what the (c,i,s) does !
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #11
"Evertjan." <ex************ **@interxnl.net > writes:
Fox wrote on 11 nov 2003 in comp.lang.javas cript:
String.prototyp e.ucwords = function(){
return this.replace(/\b\w/gi, function(c,i,s) { return
c.toUpperCase() ; });
}

String.prototyp e.ucwords = function(){
return this.toLowerCas e().replace(/\b\S/g, function(x) { return
x.toUpperCase() ; });
}


Your version would uppercase[1] any non-space letter after a word
boundary. The previous version uppercased all word characters after
a word boundary. Since word characters includes all letters, and only
letters can be meaninfully uppercased, there isn't any significant difference.
The rest has to be lowercase, IMHO.
What rest? Your RegExp matches more characters than the previous one.
I do not think the i in /gi is necessary.
Correct, \w matches both upper- and lowercase letters already.
Why replace the whole word?
It doesn't, it only replaces one character at a time. The \w code
matches *one* word character, not an entire word.
Please explain for us mortals what the (c,i,s) does !


From ECMA-262 section 15.5.4.11:
---
If /replaceValue/ is a function, then for each matched substring, call
the function with the following m + 3 arguments. Argument 1 is the
substring that matched. If /searchValue/ is a regular expression, the
next m arguments are all of the captures in the /MatchResult/ (see
15.10.2.1). Argument m + 2 is the offset within /string/ where the
match occurred, and argument m +3 is /string/. The result is a string
value derived from the original input by replacing each matched
substring with the corresponding return value of the function call,
converted to a string if need be.
---
I.e.,
c is the current match
and since the regExp doesn't contain capturing parentheses (m==0),
i is the offset of the match in the original string, and
s is the original string.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #12
Lasse Reichstein Nielsen wrote on 11 nov 2003 in comp.lang.javas cript:
It doesn't, it only replaces one character at a time. The \w code
matches *one* word character, not an entire word.


Rereading the specs ....., Yes, true.

Please explain for us mortals what the (c,i,s) does !


From ECMA-262 section 15.5.4.11:
---
If /replaceValue/ is a function, then for each matched substring, call
the function with the following m + 3 arguments. Argument 1 is the
substring that matched. If /searchValue/ is a regular expression, the
next m arguments are all of the captures in the /MatchResult/ (see
15.10.2.1). Argument m + 2 is the offset within /string/ where the
match occurred, and argument m +3 is /string/. The result is a string
value derived from the original input by replacing each matched
substring with the corresponding return value of the function call,
converted to a string if need be.
---
I.e.,
c is the current match
and since the regExp doesn't contain capturing parentheses (m==0),
i is the offset of the match in the original string, and
s is the original string.


That is all new to me, very usefull.

Quick try:

s = "a b c d e f".replace(/\w/g, function(c,i,s)
{ return "&lt;"+c+"="+i+ "+"+s+"<br> "});

document.write( s);

Nice.

Do you realy have to specify any not used arguments in function(c,i,s) ?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #13
"Evertjan." <ex************ **@interxnl.net > writes:
Do you realy have to specify any not used arguments in function(c,i,s) ?


No. Javascript functions generally don't have a fixed arity - you can
pass any number of arguments to any function. The formal parameter
names only give you names to refer more easily to the actual arguments.

So, you can use
function(c) { ... }
or even
function() {
var c = arguments[0];
...
}

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3156
by: Derek Rhodes | last post by:
using Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 OK, I have a recursive function that should return a list, but doesn't <start session> def test(word): if type(word) == str:
10
3214
by: Nige | last post by:
Repost - as some readers took got sidetracked. To save me re-inventing the wheel, does anyone have a function to capitalise each word in form data? I was thinking along the lines of capitalise the first letter of the string, then any other letter after a space or hyphen. So many users seem to think that just because they are online they can use addresses like "8 brown road, chatham, kent" and so I have to manually change them.
7
6817
by: John Aspinall | last post by:
Hi, Can someone please tell me how to capitalise all words in a string that are seperated by spaces. The string is variable and not the same every time hence I cant use the Ucase function as I wont know the exact position of the start of each word. Cheers....John
11
2713
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
4
1939
by: Christine | last post by:
I am having the strangest problem. I use the Date function in several of my forms and modules in an Access 2000 mdb. Lately, wherever in my code (in this one mdb) I use the Date function, it changes to date (lower case vs proper case). I can fix this very temporarily by reconstructing the database (importing all objects to a new mdb) or using the decomplie option to open it. But in either case, once I compile it reverts back to lower case....
6
2682
by: Ryan Muller | last post by:
My company just upgraded from Access 97 to Access 2003 today and we are having some issues in a database that generates a Word document from information selected in a form. Here is the code we were using without issue previous to the switch: 'Start Word and create a new doc from the template Set objWord = CreateObject("Word.Basic") objWord.AppMaximize "Microsoft Word", 1 objWord.FileNew Template:=StrTemplPath & "letter.dot"
0
2345
by: serkan | last post by:
Guys, I am trying to get this password reset functionality wor for me but I am not successful at all. Please somebody help me. I get "Your password could not be reset - please try again later" so I think the get_random_word function is not working right. Here are the scripts: This is the script after the user enters his/her userid. <?php require_once("bookmark_fns.php"); // creating short variable name $username = $_POST;
3
2027
by: Russell | last post by:
Hey, ok i have numerous tables to search through for a 'site search'. some of the searchble fields have html embeded within so after some quick referencing, saw I can use the regExp function to strip out all the HTML leaving only the raw text. (done and works a treat) My issue is:
4
1452
by: hanseymoon | last post by:
Dear newsgroup: I've got this long function, which works good overall to spell check words from a dictionary and I am not in a position to replace it. Can someone please see where or how it might be chopping up words like: don't. It brings them back as: 't, chopping off the "don" before the apostrophe. I've looked over the whole situation and ran many $string tests....and it appears to be narrowed it down to this. I may be wrong and...
11
1779
by: Daniel T. | last post by:
The function below does exactly what I want it to (there is a main to test it as well.) However, I'm curious about ideas of making it better. Anyone interested in critiquing it? void formatText( const string& in, int charsPerLine, int lines, vector< string >& out ) { out.resize( 1 ); out = ""; int prev = 0;
0
8968
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8787
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.