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 2 of 2

Thread: RegExp

  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts

    RegExp

    For a string of the formats: abc_def_ghl or abcd_xyz, abc_def_ghl_k, etc

    I like to replace the "_char" with the uppercased char, for example:

    abc_def_ghl --> abcDefGhl

    abcd_xyz --> abcdXyz

    abc_def_ghl_k --> abcDefGhlK

    etc.

    What is the RegExp for it?


    Thanks

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Try this
    Code:
    var myString = "abcd_ef_g_hij";
    myString = myString.replace(/([a-z]*)(_)([a-z])/g, function(match, p1, p2, p3) {return p1+p3.toUpperCase();});
    console.log(myString); // will give you abcdEfGHij


  •  

    Posting Permissions

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