String to Hump Function Example: Border-left-width-borderLeftWidth
1 //General Method2 functionFntocamel (str) {3 vararr = Str.split ('-'),4Len =Arr.length,5 i;6 for(vari = 1; i < Len; i++){7Arr[i] = Arr[i].charat (0). toUpperCase () + arr[i].substring (1);8 }9 returnArr.join (");//Note the empty string is comma by defaultTen } One //Regular A functionFntocamelre (str) { - varRe =/-(\w)/G; - returnStr.replace (Re,function(arg0, arg1) { the returnarg1.touppercase (); - }); -}
Gets the most occurrences of characters and times in a string
1 //General Method2 functionFngetmost (str) {3 varobj = {},4Len =Str.length,5num = 0,6 I,7 Char;8 9 for(vari = 0; i < Len; i++){Ten if(Obj[str[i]]) { One Obj[str[i]].push (Str[i]); A}Else{ -Obj[str[i]] =[Str[i]]; - } the } - - for(Iinchobj) { - if(Num <obj[i].length) { +num =obj[i].length; - Char= Obj[i][0]; + } A } at - return { - Char:Char, - Num:num - }; - } in - //Regular to functionFngetmostre (str) { + vararr = str.split (' '), -Re =/(\w) \1+/, thenum = 0, * Char; $ Panax Notoginseng Arr.sort (); -str = arr.join (' '); the +Str.replace (Re,function(arg0, arg1) { A if(Arg0.length >num) { thenum =arg0.length; + Char=arg1; - } $ }); $ - return { - Char:Char, the Num:num - };Wuyi}
Add a substring to a string for example: 1234567890-123,456,789
1 //General Method2 functionFngetthousand (str) {3 varLen =Str.length,4num = len% 3,5arr = [],6 i;7 8 if(num > 0) {//Mr. Foo the extra characters starting with the string as a whole into the array9Arr.push (str.substring (0, num));Ten } One Astr = str.substring (num);//Remove the extra characters -Len =str.length; - the for(i = 0; i < len; i + = 3) {//three-A-byte deposit array -Arr.push (Str.substr (i, 3)); - } - returnArr.join (); + } - + //Regular A functionFngetthousandre (str) { at varRe =/(? = (?! \b) (\d{3}) +$)/G; - - returnStr.replace (Re, ', '); -}
Several questions of JS's face