Skip to content

Instantly share code, notes, and snippets.

@langjt
Last active July 3, 2018 09:56
Show Gist options
  • Save langjt/32644ea89c2abe3b393a to your computer and use it in GitHub Desktop.
Save langjt/32644ea89c2abe3b393a to your computer and use it in GitHub Desktop.
// 方法一:split
function strHump(str) {
if (typeof str !== 'string') {
return str;
}
var arr = str.split('-');
for (var i= 1,len=arr.length; i<len; i++) {
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
}
return arr.join('');
}
// 方法二:正则
function strHumpReg(str) {
if (typeof str !== 'string') {
return str;
}
return str.replace(/-(\w)/g, function ($0, $1) {
return $1.toUpperCase();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment