Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    São Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts

    replace with toLowerCase

    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:
    Code:
    textStr = "s_SubItem";
    document.write(textStr+"<br>");
    textStr = textStr.replace(/\w{1}\_([a-zA_Z])/gi, "$1".toLowerCase());
    document.write(textStr+"<br>");
    But it's still uppercase afterwards (SubItem). Any suggestions?
    /Daniel

  • #2
    Regular Coder COBOLdinosaur's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    400
    Thanks
    2
    Thanked 30 Times in 30 Posts
    textStr=textStr.toLowerCase();

    Should be all you need to convert the string.
    100% standards compliant code is 100% correct 100% of the time.
    one of my toys from my repository and perhaps some help getting help

    Cd&

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    São Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    /Daniel

  • #4
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    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;
    alert(mystr);
    </script>

    Basscyst

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Regular Coder
    Join Date
    Aug 2002
    Location
    São Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Basscyst: Thank you, your method works as long as only one match is required. Unfortunately there will almost always be serveral. Thanks anyway.

    liorean:
    That's what I was looking for. Though it didn't replace s_SubItem, a small adjustment made it work:
    Code:
    textStr = textStr.replace(/\w{1}\_([a-z])/gi, function(s1,p1){return p1.toLowerCase();});
    Thanks!

    Edit: Unfortunately it didn't work in IE5.0...
    Last edited by Danne; 10-24-2003 at 07:09 PM.
    /Daniel

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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?
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Regular Coder
    Join Date
    Aug 2002
    Location
    São Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

    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.
    /Daniel

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    A long way to do it, but look at the methodology used in <http://codingforums.com/showthread.p...hlight=liorean>.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #10
    Regular Coder
    Join Date
    Aug 2002
    Location
    São Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That could be useful. Thanks.
    /Daniel


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •