469,950 Members | 2,361 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

lower case conversion in replace function

Hi,

I'trying to use a regExp in Javascript to replace any all upper case word in a string by the same word having only its first letter in upper case.
This expression detects the words:
/([A-Z])([A-Z]+)/g
and backreferences $1 and $2 will contain the first letter and the rest of the word respectively.

Now, how can I convert $2 to lower case in the replace function?
I would need something like:
replace(/([A-Z])([A-Z]+)/g, "$1" + "$2".toLowerCase())
But this does not work since the toLowerCase() is applied first to the string "$2", THEN the string $2 is replaced by the second match.

Jul 20 '05 #1
3 3431
Claude Schneegans wrote:
Now, how can I convert $2 to lower case in the replace function?
I would need something like:
replace(/([A-Z])([A-Z]+)/g, "$1" +
"$2".toLowerCase())
But this does not work since the toLowerCase() is applied first to
the string "$2", THEN the string $2 is replaced by the second match.


Try:

var s = "ABCDE FGHI";
s = s.replace(
/([A-Z]+)/g,
function (w) {
return w.charAt(0).toUpperCase() +
w.substring(1).toLowerCase();
}
);
alert(s);
HTH;
JW

Jul 20 '05 #2
@SM


Claude Schneegans a ecrit :

Hi,

I'trying to use a regExp in Javascript to replace any all upper case word in a string by the same word having only its first letter in upper case.
This expression detects the words:
/([A-Z])([A-Z]+)/g
and backreferences $1 and $2 will contain the first letter and the rest of the word respectively.
truc=truc.charAt(0).toUpperCase()+truc.substring(1 ,truc.length).toLowerCase;

Now, how can I convert $2 to lower case in the replace function?


$something is not accepted in JS language !

--
******** (enlever/remove [OTER_MOI] du/from reply url) *******
Stphane MORIAUX : mailto:st*********************@wanadoo.fr
Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS)
http://perso.wanadoo.fr/stephane.moriaux/internet/
************************************************** ************
Jul 20 '05 #3
"@SM" <st*********************@wanadoo.fr> writes:
$something is not accepted in JS language !


Is so! :P

Javascript identifiers may contains the letters (a-z, any case),
digits (0-9), underscore (_) and dollar sign ($). The first character
may not be a digit. Read ECMA 262, section 7.6.

The $ is *intended* for mechanically generated code, but there is no
requirements.

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Hallvard B Furuseth | last post: by
22 posts views Thread by DJ | last post: by
17 posts views Thread by Janice | last post: by
11 posts views Thread by Peter Oliphant | last post: by
19 posts views Thread by Eric Lindsay | last post: by
9 posts views Thread by B Williams | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.