Capitalize First Letter Exlcuding ( ) and /

G

gil

I have a script that will capitalize the first letter of every word in
my descriptions. But some of my descriptions contain ( ) and /. For
example bezel (black). I need the script to capitalize Bezel and
(Black), but it is only finding the bezel and leaving the (black) in
lower case. Here is the script below. Any help would be greatly
appreciated.

<SCRIPT LANGUAGE="JAVASCRIPT">
<!--

function capitalizeMe(obj) {
val = obj.value;
newVal = '';
val = val.split(' ');
for(var c=0; c < val.length; c++) {
newVal += val[c].substring(0,1).toUpperCase() +
val[c].substring(1,val[c].length) + ' ';
}
obj.value = newVal;
}

// -->
</SCRIPT>
 
M

Martin Honnen

I have a script that will capitalize the first letter of every word in
my descriptions. But some of my descriptions contain ( ) and /. For
example bezel (black). I need the script to capitalize Bezel and
(Black), but it is only finding the bezel and leaving the (black) in
lower case.

The following works with IE 5.5 and later (respectively JScript 5.5 and
later), with Netscape 4.06 and later (respectively JavaScript 1.3 and
later), and other ECMAScript edition 3 compliant implementations:

var string = 'bezel (black)';
string = string.replace(/\b(\w)/g,
function (w, p1) { return p1.toUpperCase(); });
alert(string);
 
S

Stephen Chalmers

I have a script that will capitalize the first letter of every word in
my descriptions. But some of my descriptions contain ( ) and /. For
example bezel (black). I need the script to capitalize Bezel and
(Black), but it is only finding the bezel and leaving the (black) in
lower case. Here is the script below. Any help would be greatly
appreciated.

You need to scan for and record the position of the first alphabetical
character, which then you use as the start point for the substring.

<script type='text/javascript'>

function capitaliseMe(obj)
{
val = obj.value;

newVal = '';

val = val.split(' ');

for(var c=0; c < val.length; c++)
{
for( var i=0; i<val[c].length && !/[A-Za-z]/.test(val[c].charAt(i));
i++ )
;
newVal += val[c].substring( 0, i )+
val[c].substring( i, i+1 ).toUpperCase()+
val[c].substring( i+1, val[c].length ) + ' ';
}

obj.value = newVal;

}
</script>
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Fri, 27 May 2005 08:35:36, seen in (e-mail address removed) posted :
I have a script that will capitalize the first letter of every word in
my descriptions. But some of my descriptions contain ( ) and /. For
example bezel (black). I need the script to capitalize Bezel and
(Black), but it is only finding the bezel and leaving the (black) in
lower case. Here is the script below. Any help would be greatly
appreciated.
function capitalizeMe(obj) {
val = obj.value;
newVal = '';
val = val.split(' ');
for(var c=0; c < val.length; c++) {
newVal += val[c].substring(0,1).toUpperCase() +
val[c].substring(1,val[c].length) + ' ';
}
obj.value = newVal;
}


Please do not let your software wrap your code lines.

The following seems simple, though it requires one concatenation per
character rather than two per changed character. If you do not want to
capitalise the k of 9kt, alter the expression for WL accordingly.

S = "aaa bbb (ccc) ddd 9kt"
T = ''

WL = false
for (j=0; j<S.length; j++) {
C = S.charAt(j)
UC = C.toUpperCase()
T += WL ? C : UC
WL = UC>="A" && UC<="Z"
}

For general Internet use, you should use your own toUpperCase and adjust
WL in order to handle letters that Americans do not know about.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top