JS: Regular Expression-email address verification Parsing

Source: Internet
Author: User

From: http://www.cnblogs.com/Dannier/archive/2010/11/06/RegExp.html

Email Verification



A regular expression (often abbreviated as Regexp) is a mode written with special symbols to describe one or more text strings. The regular expression is used to match the text pattern, so that the script can easily identify and operate the text. In fact, regular expressions are worth your time. Regular expressions are not only useful in Javascript, but can also be used in many other places, such as other programming languages (such as Perl, Java, C #, Python, and PHP ), apache configuration file and text editors such as bbedit and textmate. Even Adobe Dreamweaver
And Microsoft Word (to a certain extent) using regular expressions can also achieve more powerful search and replacement.

The following is a regular expression used to verify the email.

VaR Re =/^ \ W + ([\.-]? \ W +) * @ \ W + ([\.-]? \ W +) * (\. \ W {2, 3}) + $ /;

Next, we will analyze this regular expression.

Re is a variable used to store the regular expression on the right. In JavaScript, the variable is declared using the VaR keyword.

The reading sequence of the regular expression is from left to right.

Regular Expressions always start and end with a slash (/). All content between the slashes is part of the regular expression.

The Escape Character (^) indicates that we want to use this expression to check strings starting with a specific string. If the escape character is removed, the email address may be considered valid even if the string starts with a pile of spam characters.

Expression \ W represents any single character, including ~ Z, ~ Z, 0 ~ 9 or underline. Email must start with one of these characters.

The plus sign + indicates that we want to search for the noodles to appear once or multiple times.

Parentheses () indicate a group, which means that all content in the parentheses will be referenced later, so now they are placed in a group.

Brackets [] are used to indicate any character that can appear. In this example, square brackets contain characters \.-. We want to allow users to enter a dot or a hyphen, but the DOT has special significance for the regular expression, so we need to add a backslash \, add a backslash before a special character to indicate "Escape Character", and the character after escape indicates its own meaning. Because there are square brackets, the input string can have a dot or a hyphen at this position, but the two types cannot exist at the same time.

Question mark? Indicates that the previous entry can appear once or not. Therefore, the first part of the email address can contain a dot, a hyphen, or no.

In? Use \ W + again to indicate that the DOT or hyphen must be followed by other characters.

* After () indicates that the previous entry can appear zero or multiple times. Therefore, the content in parentheses can appear zero or multiple times.

The @ character represents itself and has no other meaning. It is located between the email address and domain name.

The @ character is followed by the \ W + character, indicating that the @ character must be followed. After that, it appears again ([\.-]? \ W +) * indicates that the suffix of the email address can contain periods or hyphens.

Then, create another group (\. \ W {2, 3}) in a pair of parentheses, indicating that we want to find a dot, followed by some characters. In this example, the numbers in braces indicate that the preceding entries can appear two to three times. The Group is followed by a plus sign (+), indicating that the previous entry (This group) must appear once or multiple times. This will Match. com,. Edu, and so on, as well as ox. ac. uk.

Finally, the regular expression ends with a dollar sign $, indicating that the matched string must end here. The regular expression that ends with a slash.

JS Code:

Return re. Test (STR );


C #:

Using system. Text. regularexpressions;

# Enter a number in the region verification text box
/// <Summary>
/// Verify if it is a number (including integers and decimals)
/// </Summary>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Public static bool getnum (string Str)
{
Return RegEx. ismatch (STR, @ "^ [-]? \ D + [.]? \ D * $ ");
}
# Endregion

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.