View Full Version : replace with toLowerCase

10-23-2003, 09:54 PM
How can I replace something that is in upper case and put it into lower case?

Ex 1. 3_Content should be content

Ex 2. s_SubItem should be subItem

I created a RegExp like this:

textStr = "s_SubItem";
textStr = textStr.replace(/\w{1}\_([a-zA_Z])/gi, "$1".toLowerCase());

But it's still uppercase afterwards (SubItem). Any suggestions?

10-23-2003, 11:40 PM

Should be all you need to convert the string.

10-23-2003, 11:54 PM
Thank you for responding, but then I would convert the whole string:

SubItem would be subitem,
when I want it to be subItem.

I could of course convert only the first character, but subItem is inside a bigger text. The only way to find it through this pattern:
(/\w{1}\_([a-zA_Z])/gi, as mentioned in previous post.

10-24-2003, 01:44 AM
Hello Danne -

From what I've read it seems like this should work.

<script language="javascript">
var str=("sdkSACSCJKSDHCVKSjhdvkljsvhss_SubItemsdkjhkchskdcasc");
var findstart=str.indexOf("s_SubItem");
var findend=findstart+9;
var str=str.substring(findstart+2,findend);
var get_s=str.substring("0","1");
var lows=get_s.toLowerCase();
var str=str.substring("1",str.length);
var mystr=lows+str;


10-24-2003, 02:02 AM
Oh, this is on of the problems I've asked about on this forum once...
In short, "$1".toLowerCase() is executed BEFORE the replacement of $1 with the first matched subexpression. You can send it to a function instead:

textstr = textstr.replace(/\w{1}\_([a-z])/gi, function(s1){return s1.toLowerCase();});

Just check that it works in your target browsers first, of course. I know it works in moz, op7 and ie6w.

10-24-2003, 08:07 PM
Basscyst: Thank you, your method works as long as only one match is required. Unfortunately there will almost always be serveral. Thanks anyway. :)

That's what I was looking for. Though it didn't replace s_SubItem, a small adjustment made it work:

textStr = textStr.replace(/\w{1}\_([a-z])/gi, function(s1,p1){return p1.toLowerCase();});


Unfortunately it didn't work in IE5.0...:(

10-24-2003, 08:20 PM
Well, I can't go give you code that works for a five years old browser, can I? How well does saf and iem handle it?

10-24-2003, 09:00 PM
Originally posted by liorean
Well, I can't go give you code that works for a five years old browser, can I? How well does saf and iem handle it?

I didn't really ask you for it either, did I? :confused:

Thanks for your help on previous post. I think I will give up from doing it, since it in fact is made for JScript in ASP. IE5.0 matches the version we are running.

10-24-2003, 09:21 PM
A long way to do it, but look at the methodology used in <http://codingforums.com/showthread.php?s=&threadid=15452&highlight=liorean>.

10-27-2003, 02:24 PM
That could be useful. Thanks.