JavaScript Learning Notes (eight) regular expression _ basics

Source: Internet
Author: User

Basic concepts

A regular expression is a text pattern that includes ordinary characters (for example, letters between A and z) and special characters (called "metacharacters"). The pattern describes one or more strings to match when searching for text.

First, we recommend several regular expression editors
debuggex:https://www.debuggex.com/
pyregex:http://www.pyregex.com/
regexper:http://www.regexper.com/

A regular expression is a lookup and a string substitution operation. Regular expressions are widely used in text editors, such as regular expressions used to:
[Copy] Check that the text contains the specified characteristic word
Find the location of matching feature words in the text
Extract information from text, such as a substring of a string
Modify Text

Description: Regular expressions are typically used for two tasks: 1. Validation, 2. Search/Replace. When used for validation, it is usually necessary to add ^ and $ separately before and after to match the entire string to be validated, and if the search/replace plus this qualification is based on the search requirements, it is also possible to add \b instead of ^ and $. The commonly used regular expressions listed in this table are not subject to any qualification, except for a few exceptions, and are handled on your own request.

Priority order

After a regular expression is constructed, it can be evaluated like a mathematical expression, that is, it can be evaluated from left to right and in a priority order. The following table lists the order of precedence for various regular expression operators from highest priority to lowest priority:

operator Description
\ Escape character
(), (?:), (?=), [] Parentheses and square brackets
*, +,?, {n}, {n,}, {n,m} Qualifier
^, $, \anymetacharacter Location and order

Establish regular expressions

The method for constructing regular expressions is the same as for creating mathematical expressions. That is, using multiple metacharacters and operators to combine small expressions to create larger expressions.
You can construct a regular expression by putting together various components of an expression pattern between a pair of delimiters.

For JScript, the delimiter is a pair of forward slash (/) characters. For example:
/expression/

For VBScript, a pair of quotation marks ("") is used to determine the bounds of the regular expression. For example:

Copy Code code as follows:

"Expression"

See an example

Copy Code code as follows:

Match account number is legal (start letter, allow 5-16 bytes, allow alphanumeric underline

var re =new RegExp ("^[a-za-z][a-za-z0-9_]{5,19}$");
if (Re.test (AAAA)) {
Alert ("well-formed");
}else{
Alert ("Malformed");
}

The component of a regular expression can be a single character, character set, character range, selection between characters, or any combination of any of these components.

Regular expressions that are commonly used

Matching regular expressions for Chinese characters: [\U4E00-\U9FA5]
Commentary: Matching Chinese is really a headache, with this expression will be easy to do

Match Double-byte characters (including Chinese characters): [^\x00-\xff]
Commentary: can be used to compute the length of a string (a double-byte character length meter 2,ascii 1 characters)

A regular expression that matches a blank row: \n\s*\r
Commentary: can be used to delete blank lines

Regular expression:< matching HTML tags (\s*?) [^>]*>.*?</\1>|<.*? />
Commentary: The online version is too bad, the above can only match the part of the complex nested tags still powerless

A regular expression that matches the end-end whitespace character: ^\s*|\s*$
Commentary: A useful expression that can be used to delete white-space characters (including spaces, tabs, page breaks, and so on) at the end of a line at the beginning

Regular expression matching an email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *
Commentary: Form validation is useful

Regular expressions that match URL URLs: [a-za-z]+://[^\s]*
Commentary: Online circulation of the version of the function is very limited, which can meet the basic requirements

Match account number is legal (beginning of letter, allow 5-16 bytes, allow alphanumeric underline): ^[a-za-z][a-za-z0-9_]{4,15}$
Commentary: Form validation is useful

Match domestic phone number: \d{3}-\d{8}|\d{4}-\d{7}
Commentary: Match form such as 0511-4405222 or 021-87888822

Matching Tencent QQ Number: [1-9][0-9]{4,}
Commentary: Tencent QQ number starting from 10000

Match China ZIP Code: [1-9]\d{5} (?! \d)
Commentary: China postal code is 6 digits

Matching ID: \d{15}|\d{18}
Commentary: China's ID card is 15-or 18-digit

Matching IP address: \d+\.\d+\.\d+\.\d+
Commentary: Useful when extracting IP addresses

Match a specific number
[Copy] ^[1-9]\d*$//matching positive integer
^-[1-9]\d*$//matching negative integers
^-? [1-9]\d*$//matching integer
^[1-9]\d*|0$//matching nonnegative integer (positive integer + 0)
^-[1-9]\d*|0$//matching non positive integer (negative integer + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$//matching positive floating-point numbers
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*) $//matching negative floating-point number
^-? ([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0) $//matching floating-point number
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$//matching nonnegative floating-point number (positive floating-point number + 0)
^ (-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)) |0?\.0+|0$//matching non-positive floating-point numbers (negative floating-point number + 0)

Commentary: useful when dealing with large amounts of data, pay attention to corrections when applied

Match a specific string
[copy]^[a-za-z]+$//Match a string of 26 English letters
^[a-z]+$//Match a string of 26 uppercase letters
^[a-z]+$//Match string consisting of 26 lowercase letters
^[a-za-z0-9]+$//Match a string of numbers and 26 English letters
^\w+$//Match A string of numbers, 26 English letters, or underscores

Commentary: Some of the most basic and commonly used expressions

Mind Mapping

Related Article

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.