0

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

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

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
  • 146,324
  • 30
  • 291
  • 331
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