The number of characters of each character

I am currently working on a password strength calculator, and then I need to know if the character appears more than once.
I know that I have to use regex like this occurance = password.match(/a/g).length to get ho many times a , but I want to do this with every character (letter, number, character).

Is there a way to do this with JS / JQuery, maybe regex, besides working with an array that contains all the characters I want to check?

+6
source share
5 answers

Something like that?

 var hello = "Hello world"; var histogram = {}; for (var i = 0, len = hello.length; i < len; i++) { var letter = hello[i]; histogram[letter] = (histogram[letter] || 0) + 1; } console.log(histogram); 

Result:

 { H: 1, e: 1, l: 3, o: 2, ' ': 1, w: 1, r: 1, d: 1 } 

Or you can use an array. Just change {} to [] .

+8
source

From @Noel Jose, answer here , you can simply run this function after converting the string to string.split('') array.

 function foo(arr) { var a = [], b = [], prev; arr.sort(); for( var i = 0; i < arr.length; i++ ){ if ( arr[i] !== prev ) { a.push(arr[i]); b.push(1); } else { b[b.length-1]++; } prev = arr[i]; } return [a, b]; } var stringToCheck = 'password'; var result = foo(stringToCheck.split('')); // result[0] contain unique array elements and result[1] contain number of occurrences of those elements for(var i = 0; i < result[0].length; i++){ console.log(result[0][i] + " : " + result[1][i]); } 

The transition to "testing" will lead to the following conclusion:

 e : 1 g : 1 i : 1 n : 1 s : 1 t : 2 
+5
source
 function rall(r, s) { var a=[],t,g=r.global; do {t=r.exec(s);if (!t) break; a.push(t);} while (g); return a; } var r=/.*?(.)(?=(.*?\1.*))/g; var res=rall(r,password); 

res will be an array of arrays containing all matches of repeated characters.

RegExp uses appearance to find out if the found character (recorded in the first group) will appear again later in the line.

A password of type secret elements will appear as:

 "[["s","s","ecret elements"], ["e","e","cret elements"], ["cre","e","t elements"], ["t","t"," elements"], [" e","e","lements"], ["le","e","ments"]]" 

The second element in each sub-array is a multiple-match character.

If there are no repetitions, the array will have a length = 0, which is easy to check, for example:

 if (rall(r,password).length==0) console.log('password is OK!'); 
+3
source

If you want to use an array based solution, you can try something like this:

 var password= "abcdsa"; var freq = []; for(var i = 0 ; i < password.length ; i++){ freq[password[i]] = (freq[password[i]] || 0)+1; } 

You repeat the password once and follow the conclusions of each character that you find. In this case, the "freq" array would have something like this:

 freq["a"] = 2; freq["b"] = 1; freq["c"] = 1; freq["d"] = 1: freq["s"] = 1; 
+3
source

Just reduce the string to a count object. Select the abbreviation with an empty object, every time a letter is found, then this letter gets +1 in the object where the index is a letter.

Made in reusable function

 function charCount(str){ return [].reduce.call(str,function(p,c){ p[c] = p[c] ? p[c]+1 : 1; return p; },{}); } charCount("hello");//Object {h: 1, e: 1, l: 2, o: 1} 
0
source

All Articles