0

I want to transform a string like config-option into configOption. What will be the easiest way?

Adrian Ber
  • 18,774
  • 9
  • 64
  • 106

2 Answers2

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();})
Community
  • 1
  • 1
p.s.w.g
  • 141,205
  • 29
  • 278
  • 318
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
  • 18,774
  • 9
  • 64
  • 106