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 5 of 5
  1. #1
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts

    regex? upper case following number

    i'm looking for a way to capitalize every letter that immediately follows a number. for example:

    1a becomes 1A
    123a456b789c becomes 123A456B789C
    a123b becomes a123B
    a123bc becomes a123Bc

    would i need to break the string up into an array and check each index or is there an easy regex that would work better?

    any ideas? thank you.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    You can do a replace with a RegExp, but I really don't know if it is more efficient than a match or exec method.

    var str='123w2345tt4yuio000zz111234rt'

    str.replace(/\d[a-z]/g,function(w){return w.toUpperCase()});

    /* returned value: (String)
    123W2345Tt4Yuio000Zz111234Rt
    */
    Last edited by mrhoo; 12-19-2008 at 03:12 AM.

  • Users who have thanked mrhoo for this post:

    oesxyl (12-20-2008)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,015
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Small correction:-

    str = str.replace(/\d[a-z]/g,function(w){return w.toUpperCase()});

  • Users who have thanked Philip M for this post:

    oesxyl (12-20-2008)

  • #4
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    thanks so much for the replies. how would i tweak this to also include letters that follow pound signs (#) and dashes (-)?

    123#a becaomes 123#A
    123-a becaomes 123-A

    thank you

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,015
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by devinemke View Post
    thanks so much for the replies. how would i tweak this to also include letters that follow pound signs (#) and dashes (-)?

    123#a becaomes 123#A
    123-a becaomes 123-A

    thank you

    Code:
    str = str.replace(/[\d#-][a-z]/g,function(w){return w.toUpperCase()}); 
    You can test your regular expressions at: http://www.claughton.clara.net/regextester.html
    Last edited by Philip M; 12-20-2008 at 08:06 AM.


  •  

    Posting Permissions

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