1.驼峰转连字符:
var s = "fooStyleCss";
s = s.replace(/([A-Z])/g,"-$1").toLowerCase();
//利用正则进行替换,简洁明了,很棒
2.转驼峰
var s1 = "foo-style-css";
s1 = s1.replace(//-(/w)/g, function(all, letter){
return letter.toUpperCase();
});
//这段2看的不是很明白
于是自己写一个,^_^,这个很容易懂吧,就是代码多了点;
var s = "style-sheet-base";
var a = s.split("-");
var o = a[0];
for(var i=1;i<a.length;i++){
o = o + a[i].slice(0,1).toUpperCase() + a[i].slice(1);
}
再写一个,这次用正则:
var s1 = "style-sheet-base";
s1 = s1.replace(//-(/w)/g, function(x){return x.slice(1).toUpperCase();});
s1 = s1.replace(//-(/w)/g, function(x){return x.slice(1).toUpperCase();});