驼峰与_互相转换
作者:互联网
//驼峰转下划线
function humpToUnderline(str){ return str.replace(/([A-Z])/g,"_$1").toLowerCase() }
//下划线转驼峰
function toCamel(str){
return str.replace(/([^_])(?:_+([^_]))/g, function ($0, $1, $2) {
return $1 + $2.toUpperCase();
})
}
//方法二
var str = 'border-bottom-color';
function change(){
var re = /_(\w)/g;
return str.replace(re,function($0,$1){
$1.toUpperCase();
})
}
alert(change(str));
//方法三
var str = 'border-bottom-color';
function change(str){
var arr = str.split('_');
for(var i=1;i<arr.length;i++){
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
}
return arr.join('');
}
alert(change(str));
标签:function,转换,驼峰,互相,replace,str,var,return,change 来源: https://www.cnblogs.com/xingqitian/p/14945463.html