Regular to determine whether the combination of numbers and letters JS code

Source: Internet
Author: User

Here is the regular of Lexrus:

The code is as follows Copy Code

/^ ([a-z]+ (? =[0-9]) | [0-9]+ (? =[a-z])) [A-z0-9]+$/ig The idea is very clear AH:

[A-z]+ (? =[0-9])

The beginning of the letter must be followed by a number.

[0-9]+ (? =[a-z]

The number begins with the letter followed.

[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:

The code is as follows Copy Code
<script type= "Text/javascript" >
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:

The code is as follows Copy Code

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 meaning is similar to above, but does not use forward check, the test code is as follows:

  code is as follows copy 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

The code is as follows Copy Code

False,true,true,false

That's right.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.