473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 3591
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) *******
Stéphane 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Hallvard B Furuseth | last post by:
Has someone got a Python routine or module which converts Unicode strings to lowercase (or uppercase)? What I actually need to do is to compare a number of strings in a case-insensitive manner,...
22
by: DJ | last post by:
Can someone tell me the library call that converts strings to lower case or retrns a new string that is lower case of the original, thanks im using <string> David
17
by: Janice | last post by:
char* line = "abcd"; How to convert the line to upper case and print? Any option for printf to do this? Thanx
11
by: Peter Oliphant | last post by:
I've been trying all morning to convert my 2003 project (managed) to 2005 (/clr since I have both managed and unmanaged code). I'm guessing I have tens of thousands of lines of code to change. Did...
19
by: Eric Lindsay | last post by:
Should HTML 4.01 Strict markup be done in upper case or in lower case? I understand that HTML allows either upper or lower case. I also notice that XHTML apparently requires lower case. However I...
0
by: n33470 | last post by:
We have a web site that is being converted from the 1.1 format into 2.0. I've noticed that after the web project has been converted, the first time that the solution is opened in VS, all of the...
3
by: bob | last post by:
In vb6 you could say s = Format(s, "!") to force text to upper case in the format function. I've searched the vb.net help system and can't find any help on formatting text. There's plenty of help...
9
by: B Williams | last post by:
I have written some code that will take in a string and print out the reverse, but I also want it to check for upper and lower case and swap them. Will someone assist me? include <iostream>...
1
by: Curious | last post by:
I'm working on a word replacement program in .NET. I have a list of words spelt in American English and need to replace them with translated British spelling while keeping the upper or lower cases...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.