[英]What's the easiest way to capitalize letters after dash in a string?
I want to transform a string like config-option into configOption . 我想将config-option之类的字符串转换为configOption 。 What will be the easiest way? 最简单的方法是什么?
Instead of match[1]
, I'd recommend match.charAt(1)
(see string.charAt(x) or string[x]? ): 我建议使用match.charAt(1)
代替match[1]
(请参阅string.charAt(x)或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();})
我只使用了正则表达式,并将替换指定为函数而不是字符串。
str.replace(/-./g, function(match) {return match[1].toUpperCase();})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.