简体   繁体   中英

What's the easiest way to capitalize letters after dash in a string?

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

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();})

我只使用了正则表达式,并将替换指定为函数而不是字符串。

str.replace(/-./g, function(match) {return match[1].toUpperCase();})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM