473,603 Members | 2,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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".toLowerCas e())
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 3612
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".toLowerCas e())
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).toU pperCase() +
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.charA t(0).toUpperCas e()+truc.substr ing(1,truc.leng th).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************ *********@wanad oo.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/rasterTriangleD OM.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
25909
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, so I assume it's simplest to convert to lower/upper first. Possibly all strings will be from the latin-1 character set, so I could convert to 8-bit latin-1, map to lowercase, and convert back, but that seems rather cumbersome.
22
6431
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
11212
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
1702
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 a lot of converting '__gc' to 'ref', converting '*' to '^', converting 'new' to 'gcnew'. Of course this can't be done with a blanket replace, as my code has both managed and unmanaged segments. Thus I have to do them one-by-one. Also, things are...
19
26434
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 saw some mention that the HTML DOM uses upper case for markup elements. So, should I worry about what this means? I am inclined to go with lower case, for two reasons. Easier to change if I subsequently want to use XHTML.
0
1388
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 aspx and ascx filenames are changed to lowercase. The filenames are not being changed during the conversion process. After conversion completes, the filenames remain unchanged. However, the first time that the solution is opened in VS2005, the...
3
11729
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 formatting numbers, dates, and times, though. Does the vb.net format function have a way to convert text to upper or lower case? Thanks.
9
2781
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> using std::cout; using std::cin; using std::endl; #include <string>
1
2505
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 of each letter in these words. For instance, I have a dictionary below: theater theatre center centre annualized annualised
0
7996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
7928
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8415
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8405
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8273
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6735
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3903
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3951
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.