Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Created May 25, 2014 16:19
Show Gist options
  • Save jikeytang/36338100da384f79ebb4 to your computer and use it in GitHub Desktop.
Save jikeytang/36338100da384f79ebb4 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140526-题目1
请写一个字符串转成驼峰的方法?
如:font-size -> fontSize, background-color -> backgroundColor.
PS:
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
// you code
```
2. 粘贴代码时请使用shift+tab,缩进前面的空白。
@karrynew
Copy link

//楼上的人写的有问题啊
var str = "border-left-color";
function camelize(s) {
    return s.replace(/-[a-z]/gi,function (c) {
        return c.charAt(1).toUpperCase();
    });
}
console.log(camelize(str));

@zjhsd2007
Copy link

"font-size".replace(/-(\w)/g,function(a,b){ return a = b.toUpperCase()})

@yanhaijing
Copy link

优化了下zjhsd2007的

"font-size".replace(/-(\w)/g,function(a,b){ return b.toUpperCase()})

@hjzheng
Copy link

hjzheng commented May 26, 2014

翻了JQuery源码:

"font-size".replace(/-([\da-z])/gi, function(all, letter ) {
        return letter.toUpperCase();
});

@styling
Copy link

styling commented May 30, 2014

replace 简直无情

@wsgouwan
Copy link

wsgouwan commented Dec 4, 2014

        function set(str){
            var re = /-(\w{1})/gi;
            return str.replace(re , function($0 , $1){
                return $1.toUpperCase() ;
            })
        }
        console.log( set('background-color') )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment