Original: JS regular judgment password type
Don't write anything today, just share a thought.
Before the discussion in the group, the Shei Liang Brothers said whether the password is pure numbers, pure letters and so on.
If you use if or switch to write to see the actual egg pain, if there are 7, 8 judge, then the old long a big paragraph.
This idea is what I learned when I looked at the JQuery source code, and I feel good about it.
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 number", "Pure Letter", "beginning of letter + any character", "number + letter any combination", "uncategorized"]; 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 the validation is done using a regular and an array.
In fact, the idea is simple, using the regular capturing 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 number"/^ ([a-za-z]+) $/corresponds to "pure letter"/^ ([a-za-z].+) $/corresponds to "letter beginning + any character"/^ ([0-9a-za-z]+) $/corresponds to "number + letter any combination"/^ ([\s\s]+) $ /corresponding to "uncategorized"
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'll just go through the result set directly from 1 and we'll know what type it is.
All right, share it today.