I want to transform a string like config-option into configOption. What will be the easiest way?
Asked
Active
Viewed 191 times
2 Answers
1
Instead of match[1]
, I'd recommend match.charAt(1)
(see string.charAt(x) or string[x]?):
str.replace(/-./g, function(match) {return match.charAt(1).toUpperCase();})
Alternatively, you can use a group in your regex:
str.replace(/-(.)/g, function(m, c) {return c.toUpperCase();})
0
I just used regular expression and specified the replacement as function not as string.
str.replace(/-./g, function(match) {return match[1].toUpperCase();})

Adrian Ber
- 20,474
- 12
- 67
- 117