JS Verify that the string is a number and the letter is regular

Source: Internet
Author: User

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.

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.