本文介绍了将任何字符串转换为驼峰大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用javascript正则表达式将字符串转换为驼峰大小写?
How can I convert a string into camel case using javascript regex?
EquipmentClass名称
或
设备类名
或设备类名
或设备类名
应该全部变为: equipmentClassName
。
推荐答案
我刚刚这样做:
String.prototype.toCamelCase = function(str) {
return str
.replace(/\s(.)/g, function($1) { return $1.toUpperCase(); })
.replace(/\s/g, '')
.replace(/^(.)/, function($1) { return $1.toLowerCase(); });
}
我试图避免将多个替换语句链接在一起。我的功能中有1美元,2美元,3美元的东西。但是这种类型的分组很难理解,你提到的跨浏览器问题也是我从未想过的。
I was trying to avoid chaining together multiple replace statements. Something where I'd have $1, $2, $3 in my function. But that type of grouping is hard to understand, and your mention about cross browser problems is something I never thought about as well.
这篇关于将任何字符串转换为驼峰大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!