469,290 Members | 1,887 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

/[HELP!]/ regxp replace uppercase/lowercase

Hi,
I need to get replace all the uppercase characters in a string with a
lowercase character and a '-'
__ie __________________________________________________
'backgroundColor' becomes 'background-color'
'borderWidth' becomes 'border-width';
-----------------------------------------------------
in PHP strtolower(ereg_replace("[A-Z]", "-\\0" , $var)); works
but obviously I'm not using php
any help much appreciated
cheers
brendan.

Jul 20 '05 #1
2 10079
brendan <br*****@justice.com> writes:
I need to get replace all the uppercase characters in a string with a
lowercase character and a '-' in PHP strtolower(ereg_replace("[A-Z]", "-\\0" , $var)); works
but obviously I'm not using php
any help much appreciated


javascript regular expressions are closer to the ones in Perl.
The closest Javascript to your PHP example would be:

var newString = oldString.replace(/[A-Z]/g,"-$&").toLowerCase();

A more exotic method (just for the fun of it) is:

var newString = oldString.replace(/[A-Z]/g,function(s){
return "-"+s.toLowerCase();
});

Both work in Netscape 4, IE6, Opera 7, and Mozilla FB 0.6.
The latter doesn't work in Netscape 3, Opera 6 or IE 5 (or earlier).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
Lasse Reichstein Nielsen wrote:
brendan <br*****@justice.com> writes:

I need to get replace all the uppercase characters in a string with a
lowercase character and a '-'


in PHP strtolower(ereg_replace("[A-Z]", "-\\0" , $var)); works
but obviously I'm not using php
any help much appreciated

javascript regular expressions are closer to the ones in Perl.
The closest Javascript to your PHP example would be:

var newString = oldString.replace(/[A-Z]/g,"-$&").toLowerCase();

A more exotic method (just for the fun of it) is:

var newString = oldString.replace(/[A-Z]/g,function(s){
return "-"+s.toLowerCase();
});

Both work in Netscape 4, IE6, Opera 7, and Mozilla FB 0.6.
The latter doesn't work in Netscape 3, Opera 6 or IE 5 (or earlier).

/L


Brilliant!
This workds perfectly thankyou so much.
b.

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Tom | last post: by
6 posts views Thread by wukexin | last post: by
3 posts views Thread by Colin J. Williams | last post: by
7 posts views Thread by Corepaul | last post: by
5 posts views Thread by Steve | last post: by
8 posts views Thread by Mark | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.