Don't write anything today, just share a thought.
In the group before the discussion, the Shei Liang brothers said to determine whether the password is pure numbers, pure letters and so on.
If you are writing with if or switch to look at the egg pain, if there are 7, 8 judgments, then the old long a big paragraph.
This is the idea I read jQuery before the source of the learned, and feel good to use in this.
Let's look at the code first.
function Test (str, RE, msg) {
var ret = Str.match (re/^ (\d+) $^ ([a-za-z]+) $^ ([a-za-z].+) $^ ([0-9a-za-z]+) $^ ([\s\s]+) $/);
var msg = msg ["", "pure digit", "pure Letter", "beginning of letter + any character", "number + letter any combination", "unclassified"];
var i = 0;
while (ret[++i] = = undefined);
return msg[i];
}
Console.log (Test ("123"));
Console.log (Test ("SSS"));
Console.log (Test ("123xx"));
Console.log (Test ("a123xx"));
Console.log (Test ("%$#@"));
You can see that using a regular and an array completes the effectiveness.
In fact, the idea is very simple, using the regular capture group to correspond to the characters in the array.
/^ (\d+) $^ ([a-za-z]+) $^ ([a-za-z].+) $^ ([0-9a-za-z]+) $^ ([\s\s]+) $/
Can be split into
/^ (\d+) $/corresponds to "pure digits"
/^ ([a-za-z]+) $/corresponds to "pure letter"
/^ ([a-za-z].+) $/corresponds to "beginning of letter + any character"
/^ ([0-9a-za-z]+) $/corresponds to "number + letter any combination"
/^ ([\s\s]+) $/corresponding "not categorized"
If the corresponding capturing group is captured, the corresponding packet will have a value, and if it is not captured it will be undefined
So we're going to go through the result set directly from 1 and we know what kind of it is.