将连字符转换为驼峰大小写(camelCase) [英] Convert hyphens to camel case (camelCase)

查看:375
本文介绍了将连字符转换为驼峰大小写(camelCase)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用正则表达式(我假设)或其他一些方法,如何转换如下内容:

With regex (i assume) or some other method, how can i convert things like:

marker-image my-example-setting markerImage myExampleSetting

我正在考虑按 - 进行拆分,然后将该超级+1的索引转换为大写。但它看起来很脏,并希望得到一些可以使代码更清晰的正则表达式的帮助。

I was thinking about just splitting by - then convert the index of that hypen +1 to uppercase. But it seems pretty dirty and was hoping for some help with regex that could make the code cleaner.

jQuery ...

No jQuery...

推荐答案

试试这个:

var camelCased = myString.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); });

正则表达式将匹配 -i marker-image 中,仅捕获 i 。然后在回调函数中将其大写并替换。

The regular expression will match the -i in marker-image and capture only the i. This is then uppercased in the callback function and replaced.

这篇关于将连字符转换为驼峰大小写(camelCase)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆