...

View Full Version : regex? upper case following number



devinemke
12-19-2008, 02:42 AM
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.

mrhoo
12-19-2008, 02:54 AM
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
*/

Philip M
12-19-2008, 07:27 AM
Small correction:-

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

devinemke
12-19-2008, 10:48 PM
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

Philip M
12-20-2008, 08:04 AM
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



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum