-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy path字符串变驼峰.js
43 lines (41 loc) · 1021 Bytes
/
字符串变驼峰.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// asd-fgh-jkl ===>asdFghJkl 小驼峰
var toCapitalize = function(str) {
return str.replace(/-\w/g, function(s) {
return s.slice(1).toUpperCase();
});
}
function firstUpperCase(str) {
return str.toLowerCase().replace(/^\S/g,function(s){return s.toUpperCase();});
}
function capitalize1(s) {
var ret = s.split('-')[0]
for (var i = 1; i < s.split('-').length; i++) {
ret += firstUpperCase(s.split('-')[i]);
}
return ret
}
function capitalize2(s) {
var ret = '';
for (var i = 0; i < s.split('-').length; i++) {
ret += firstUpperCase(s.split('-')[i]);
}
return ret
}
// 现在又出现了新需求,将[A-Z]改为[_a - _z]
function formatStr(str) {
return str.toLowerCase().replace(/^\S/g,function(s){return '_' + s.toLowerCase();});
}
function setFormat(str) {
var ret = ''
for (var i in str) {
if (/[A-Z]/.test(str[i])) {
ret += formatStr(str[i])
} else {
ret += str[i];
}
}
return ret;
}
var s = 'asd-fgh-jkl'
capitalize1(s)
setFormat(capitalize1(s))