But how can you write these functions into a regular expression? This question is really a bit nerve-racking.
Here is the regular of Lexrus:
Select All
/^ ([a-z]+ (? =[0-9]) |[ 0-9]+ (? =[a-z])) [A-z0-9]+$/ig
The idea is very clear AH:
Select All
[A-z]+ (? =[0-9])
The beginning of the letter must be followed by a number.
Select All
[0-9]+ (? =[a-z]
The number begins with the letter followed.
Select All
[a-z0-9]+
The following characters can be used as long as they are numbers or letters. After testing, found that not working, 123DD will be recognized as illegal, dd123 is legitimate, visible "number at the beginning, immediately following the letter" is not functioning. The test code is as follows:
<script type= "text/web Effects" >
function IsTrue (str) {
var reg=/^ ([a-z]+ (? =[0-9]) |[ 0-9]+ (? =[a-z])) [A-z0-9]+$/ig;
return Reg.test (str);
}
var str? = ' AABC ';
var str2 = ' aaa123 ';
var str3 = ' 123dd ';
var str4 = ' 1230923403982 ';
document.write (IsTrue (str) + ' <br/> ');
document.write (IsTrue (str2) + ' <br/> ');
document.write (IsTrue (STR3) + ' <br/> ');
document.write (IsTrue (STR4) + ' <br/> ');
</script>
The results are:
False,true,false,false
The third of the results, it is wrong to judge ' 123dd ' to be illegal. At first thought is the question of G, removed or not. Should be a browser bug, I think the Lexrus is correct, may be the browser can not handle or "|" Both sides of the is included forward check (? =).
The following revisions are followed:
/^ ([a-z]+[0-9]+) | ( [0-9]+[a-z]+)] [a-z0-9]*$/i
The meaning is similar to the above, but does not use forward check, the test code is as follows:
Full-selection Run code copy code save code
<script type= "Text/javascript" >
function IsTrue (str) {
var reg=/^ ([a-z]+[0-9]+) | ( [0-9]+[a-z]+)] [a-z0-9]*$/i;
return Reg.test (str);
}
var str? = ' AABC ';
var str2 = ' aaa123 ';
var str3 = ' 123dd ';
var str4 = ' 1230923403982 ';
document.write (IsTrue (str) + ' <br/> ');
document.write (IsTrue (str2) + ' <br/> ');
document.write (IsTrue (STR3) + ' <br/> ');
document.write (IsTrue (STR4) + ' <br/> ');
</script>
Result is
False,true,true,false
That's right.