A: implementation code:
var camelizeRE = /-(\w)/g;
function camelize(str){
return str.replace(camelizeRE,function(_,c){
return c ? c.toUpperCase():'';
})
}
camelize('kkk-zzz-hhh'); kkkZzzHzz;
II: The principle:
1.replace method:
stringObject.replace(regexp/substr,replacement)
parameter | description |
---|---|
regexp/substr | essential. RegExp object to replace or substring predetermined pattern. Note, if the value is a string, then it is as a direct amount to be retrieved text mode, without first being converted into RegExp object. |
replacement | essential. A string value. Alternatively the predetermined function generates replacement text or text. |
replace the first parameter is a regular expression, the second parameter may be a function that can accept multiple parameters. Wherein the first parameter is to capture content, the second parameter is captured set of matched (matching the number of groups, there is a corresponding number of parameters). In addition, the last two parameters may be added, the second parameter is the reciprocal of the captured content location throughout the string (for example starting from the fifth position), the last parameter is the original string. Here is an example of a web page template replacement.
Example:
1.var str = "kkk_yyy_hhh";
No string matches a regular set of matched
str.replace(''_",function(_,a,b,c){
console.log(_,a,b,c) // _ 3 kkk_yyy_hhh undefined
})
2.var str = "kkk_yyy_hhh";
Regular did not match the regular group matches
str.replace(/_/g,function(_,a,b,c){
console.log(_,a,b,c) // _ 3 kkk_yyy_hhh undefined _ 7 kkk_yyy_hhh undefined
})
3.var str = "kkk_yyy_hhh";
Regular matches have regular group matches
str.replace(/_(\w)/g,function(_,a,b,c){
console.log(_,a,b,c) // _y y 3 kkk_yyy_hhh _h h 7 kkk_yyy_hhh
})
View third case, since they will get two values _y y and will be able to achieve the "hyphen string" into a "hump named string"