Let's take a look at the regular rules.
Text
/^ (\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"
Example
The code is as follows |
Copy Code |
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", "not classified"]; var i = 0; while (ret[++i] = = undefined); return msg[i]; } |
Test
The code is as follows |
Copy Code |
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.
code is as follows |
copy code |
/^ (\d+) $|^ ( [a-za-z]+) $|^ ([a-za-z].+) $|^ ([0-9a-za-z]+) $|^ ([\s\s]+) $/ |