Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
35
rated 0 times [  39] [ 4]  / answers: 1 / hits: 88208  / 13 Years ago, tue, july 12, 2011, 12:00:00

With regex (i assume) or some other method, how can i convert things like:



marker-image or my-example-setting to markerImage or myExampleSetting.



I was thinking about just splitting by - then convert the index of that hypen +1 to uppercase. But it seems pretty dirty and was hoping for some help with regex that could make the code cleaner.



No jQuery...


More From » regex

 Answers
18

Try this:



var camelCased = myString.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); });


The regular expression will match the -i in marker-image and capture only the i. This is then uppercased in the callback function and replaced.


[#91232] Monday, July 11, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shylaelisan

Total Points: 37
Total Questions: 94
Total Answers: 110

Location: Angola
Member since Tue, May 5, 2020
4 Years ago
;