/[HELP!]/ regxp replace uppercase/lowercase

B

brendan

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.
 
L

Lasse Reichstein Nielsen

brendan said:
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
 
B

brendan

Lasse said:
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.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top