Last active
July 3, 2018 09:56
-
-
Save langjt/32644ea89c2abe3b393a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 方法一: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