code is as follows |
copy code |
function CharCount (str) { Replaces characters from a string with a regular match, using match () with a regular match. Length gets the number of occurrences of each character str = str.replace (/s/ig, "");//Remove whitespace var Strarr = Str.split (""); var result = [],reg; for (var i = 0; i < strarr.length; i++) { if (Str.indexof (Strarr[i])!=-1) { reg = new RegExp (Strarr[i], "IG"); Counts=str.match (reg). length; str = str.replace (Reg, ""); Result.push (Strarr[i] + ":" + counts); } www.111cn.net } return result; } var str= "This section of the site is dedicated to the JavaScript language itself, the parts that are not specific to Web p Ages or other host " var result=charcount (str); Console.log (Result.sort ()); |
Law 2:
Replaces characters from a string with a regular match, replacing the string length-the length of the string after the replacement, and the difference is the number of occurrences of the character
The code is as follows |
Copy Code |
function CharCount (str) { Replaces characters from a string with a regular match, replacing the string length-the length of the string after the replacement, and the difference is the number of occurrences of that character! str = str.replace (/s/ig, "");//Remove whitespace var Strarr = Str.split (""); var result = [],beforelength, Afterlength, Reg; for (var i = 0; i < strarr.length; i++) { if (Str.indexof (Strarr[i])!=-1) { Beforelength = Str.length; reg = new RegExp (Strarr[i], "IG"); str = str.replace (Reg, ""); Afterlength = Str.length; Result.push (Strarr[i] + ":" + (beforelength-afterlength)); } www.111cn.net } return result; } var str= "This section of the site is dedicated to the JavaScript language itself, the parts that are not specific to Web p Ages or other host " var result=charcount (str); Console.log (Result.sort ()); |
The 1th method uses fewer variables and is relatively well understood!