JavaScript regular expressions to determine exec () function usage

Source: Internet
Author: User
Tags regular expression


In recent projects, a variety of information is often required to format validation, of course, the most common is through the regular expression of validation,

In your previous blog, you found a way to do the following:

Mailbox format is not correct

if (!$ ("#email"). Val (). Match (/^ ([a-za-z0-9_\.\-]) +\@ (([a-za-z0-9\-]) +\.) + ([a-za-z0-9]{2,4}) +$/)) {
Alert ("Incorrect mailbox format");
$ ("#email"). focus ();
return false;
}

This is compared through the match () function, and after an attempt to find out, you can also use this function exec ()

Examples are as follows:

var mobilephone =/^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$/;

$ (". Maintenance_phone"). blur (function (event) {
var phone = $ (this). Val ();
var errormsg = ' cell phone number format is not correct ';
$ (this). Parents ('. Tips-after2 '). Siblings (". Form-tips"). Remove ();
if (!mobilephone.exec (phone)) {
$ (". Tips-after2"). After (' <p style= ' Float:left;margin-left:2px;width:105px;height:33px;line-height:33px;color : #ff4800; white-space:pre; "class=" form-tips "> ' +errormsg+ ' </p>");
}
});

Unlike the above method, when the input box loses focus, it is judged and the error message is printed in real time, so the following aspects are better for better interaction.

It also needs to be noted that if () is judged, the form is more concise by defining variables.

Additional:

Prompt for error message
var tel =/^ ([0-9]{3,4}-)? [0-9] {7,8}$/;
var idcard =/^[1-9] ([0-9]{14}|[ 0-9]{17}| ([0-9]{16}[xx]) $/i; Id
var mail =/.+@.+\. [A-za-z] {2,4}$/;

Note: These regular expressions are not enclosed in quotes ...

Some rules of regular expression are no longer described here, only the difference between exec and match is recorded:

1. Exec is a regular expression method, not a string method, and its arguments are strings, as follows:

As defined above

var reg = new RegExp ("abc");
var str = "3ABC4,5ABC6";
Reg.exec (str);

2. Match is a method for string execution to match regular expression rules, and his arguments are regular expressions such as

var reg = new RegExp ("abc");
var str = "3ABC4,5ABC6";
Str.match (REG);

3, exec and match return are all arrays;

If the regular expression executed by Exec does not have a subexpression (the contents within the parentheses, such as/ABC (\s*)/(\s*)), if there is a match, the first matching string content is returned, and the array has only one element, and if no match returns null;

var reg = new RegExp ("abc");
var str = "3ABC4,5ABC6";
Alert (reg.exec (str));
Alert (Str.match (reg));

If you execute the code, you'll find that the content is the same: ABC,

4. If the defined regular expression object is a global match such as:

var reg = new RegExp ("abc", "G");
var str = "3ABC4,5ABC6";
Alert (reg.exec (str));
Alert (Str.match (reg));

Is ABC and ABC,ABC, because match performs a global matching query, and exec returns if no subexpression finds a match.
5. When the expression contains a subexpression:

var reg = new RegExp ("A (BC)");
var str = "3ABC4,5ABC6";
Alert (reg.exec (str));
Alert (Str.match (reg));

You will find that both of the results of the execution are: ABC,BC;
6. When the regular expression object is defined as a global match

var reg = new RegExp ("A (BC)", "G");
var str = "3ABC4,5ABC6";
Alert (reg.exec (str));
Alert (Str.match (reg));

The result of both returns is ABC,BC and ABC,ABC,

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.