By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

replace help

P: n/a
CK
Hi Guys,
I need a function that will replace the letter after a "hyphen" with a
capital letter.
Like if the user enter "Zeta-jones" it would be "Zeta-Jones".
I am a little stumped. Any ideas? I know it's a combination of substring
and replace and toUpper().
I just can't get the method call correctly.

I use iHyphen = x.indexOf("-");

I need to replace the letter after the hyphen with the uppercase version of
the letter.

Any ideas?
Thanks,
Chris
Jan 10 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
CK wrote:
Hi Guys,
I need a function that will replace the letter after a "hyphen" with a
capital letter.
Like if the user enter "Zeta-jones" it would be "Zeta-Jones".
I am a little stumped. Any ideas? I know it's a combination of substring
and replace and toUpper().
I just can't get the method call correctly.

I use iHyphen = x.indexOf("-");

I need to replace the letter after the hyphen with the uppercase version of
the letter.
You can use the following function based on a post from Baconbutty[1]:

var s="Zetta-jones-smith-walker";
var r=/(-)([a-z])/g;
s = s.replace(r,function(a,b,c){return '-' + c.toUpperCase();});
alert(s);

But note that it will not work in old browsers (or some newer ones), but
that is fixable (follow the thread down).

1.
<URL:
http://groups.google.co.uk/group/com...776c776ce21e48

--
Rob
Jan 11 '06 #2

P: n/a
CK
Unfortunately that strips out the hyphens. Thanks. Here's what I came up
with.

iHyphen = x.indexOf("-");
if(iHyphen > - 1)
{
output = x.replace(x.substr(iHyphen+1,
1),x.substr(iHyphen+1,1).toUpperCase());

}


"RobG" <rg***@iinet.net.au> wrote in message
news:um****************@news.optus.net.au...
CK wrote:
Hi Guys,
I need a function that will replace the letter after a "hyphen" with a
capital letter.
Like if the user enter "Zeta-jones" it would be "Zeta-Jones".
I am a little stumped. Any ideas? I know it's a combination of
substring and replace and toUpper().
I just can't get the method call correctly.

I use iHyphen = x.indexOf("-");

I need to replace the letter after the hyphen with the uppercase version
of the letter.


You can use the following function based on a post from Baconbutty[1]:

var s="Zetta-jones-smith-walker";
var r=/(-)([a-z])/g;
s = s.replace(r,function(a,b,c){return '-' + c.toUpperCase();});
alert(s);

But note that it will not work in old browsers (or some newer ones), but
that is fixable (follow the thread down).

1.
<URL:
http://groups.google.co.uk/group/com...776c776ce21e48

--
Rob

Jan 11 '06 #3

P: n/a
CK wrote:

Please dont' top-post.

Unfortunately that strips out the hyphens.
The original does, the version I posted doesn't (see below).

Thanks. Here's what I came up
with.

iHyphen = x.indexOf("-");
if(iHyphen > - 1)
{
output = x.replace(x.substr(iHyphen+1,
1),x.substr(iHyphen+1,1).toUpperCase());

}
If your hyphenated words may ever have more than one hyphen, or if the
first letter may not be already capitalised, then:

function hyCase(str)
{
var s=[], ss=str.split('-'), i=ss.length;
while ( i-- ){
s[i] = ss[i].substr(0,1).toUpperCase() + ss[i].substr(1);
}
return s.join('-');
}

alert(hyCase('zetta-jones-smith-walker'));
may suit better.


"RobG" <rg***@iinet.net.au> wrote in message
news:um****************@news.optus.net.au...


[...]

You can use the following function based on a post from Baconbutty[1]:

var s="Zetta-jones-smith-walker";
var r=/(-)([a-z])/g;
s = s.replace(r,function(a,b,c){return '-' + c.toUpperCase();});
alert(s);

But note that it will not work in old browsers (or some newer ones), but
that is fixable (follow the thread down).

1.
<URL:
http://groups.google.co.uk/group/com...776c776ce21e48

--
Rob
Jan 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.