JS Regular Expression Learning and summary (Must see article) _ Regular expression

Source: Internet
Author: User
Tags lowercase modifier sub domain

Recently in doing a small project with a regular match, feeling is very good, so I intend to take time to a small sum.

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. The RegExp object represents a regular expression, which is a powerful tool for performing pattern matching on strings. A regular expression is a lookup and a string substitution operation.

New Regular expression

Mode one: Direct quantity grammar

var reg =/pattern/attributes

Mode two: Create the syntax of the REGEXP object

var reg = new RegExp (pattern, attributes);

Parameter description:

Parameter pattern is a string that specifies the schema or other regular expression of the regular expression.
The parameter attributes is an optional string that contains the attributes "G", "I" and "M", which are used to specify global matches, case-sensitive matches, and multiline matching. The M attribute is not supported until ECMAScript is standardized. If pattern is a regular expression, rather than a string, you must omit the argument.

The difference between the two is:
1. A new regular expression object with direct-volume syntax is generated when the code is compiled, which is commonly used in the development of the normal way;
2. A regular object that is generated using a constructor is generated when the code is run.

Regular Expressions use:
The method of a regular object refers to the use of: RegExp object. Method (String)
String objects are used in this way: strings. Method (RegExp object)

Properties and methods of regular objects

Property

IgnoreCase returns a Boolean value indicating whether the RegExp object has a flag I
Global returns a Boolean value indicating whether the RegExp object has a flag g
Multiline returns a Boolean value indicating whether the RegExp object has a flag m.
Lastindex An integer that identifies the character position to begin the next match
Source returns the original text of the regular expression (excluding backslashes)

I performs an insensitive match to the case

G performs a global match (finds all matches rather than stops after the first match is found).
m to perform multiple line matches
The role of regular expressions

Typically used for two tasks:

1. Verify
When used for validation, it is usually necessary to add ^ and $ separately before and after to match the entire string to be validated;

2. Search replacement
If the search/replace is combined with this qualification, depending on the requirements of the search, in addition, it is possible to add \b instead of ^ and $

Character class matching

[...] Find any character between square brackets
[^ ...] Find any characters that are not between brackets
[A-z] find any character that writes a to lowercase z from small
[A-z] find any character from uppercase A to uppercase Z
[A-z] find any character from uppercase A to lowercase Z
. Find a single character, except for line breaks and line ends
\w lookup Word characters, equivalent to [a-za-z0-9]
\w find non word characters, equivalent to [^a-za-z0-9]
\s Find white space characters
\s Find Non-white-space characters
\d Lookup number, equivalent to [0-9]
\d Lookup of Non-numeric characters, equivalent to [^0-9]
\b Match word boundaries
\ r Find a carriage return character
\ t Find tabs
To find NULL characters
\ n Find line breaks

Repeat character matching

{N,m} matches the previous item at least n times, but not more than m times
{n,} matches n times or more times before
{n} matches previous n times
N? Matches the previous item 0 or 1 times, which means the previous item is optional, equivalent to {0,1}
n+ matches 1 or more times before, equivalent to {1,}
n matches the previous item 0 or more times, equivalent to {0,}
n$ matches any string ending with n
^n matches any string that starts with n
? =n matches any string immediately followed by the specified string n
?! n matches any string that is not followed by the specified string n

Match a specific number

^[1-9]\d*$ matching positive integer
^-[1-9]\d*$ matching negative integers
^-? [0-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$ matching non-positive floating-point numbers (negative floating-point number + 0)

Match a specific string

^[a-za-z]+$ matches a string of 26 English letters
^[a-z]+$ matches a string of 26 English-letter Capitals
^[a-z]+$ matches a string consisting of 26 lowercase letters
^[a-za-z0-9]+$ matches a string of numbers and 26 English letters
^\w+$ matches a string of numbers, 26 English letters, or underscores

Method

Test method

Retrieves the value specified in the string. Returns TRUE or FALSE.
Returns True if string strings contain text that matches regexpobject, or false.

Demo1:

If the regular expression has a G modifier, each time the test method matches at the end of the last match.

A regular expression using the G modifier, which indicates the location of each search to be recorded, then uses the test method, where each start of the search is the last match.

<! DOCTYPE html>
 
 

Demo2:

If the regular expression is an empty string, all strings are matched, but you need to use the new RegExp () method

<! DOCTYPE html>
 
 

exec method

The EXEC () method retrieves the matching of regular expressions in a string.

Returns an array that holds the result of the match. If no match is found, the return value is null.

Demo1:

<! DOCTYPE html>
 
 

Demo2:

If the regular expression contains parentheses, the returned array includes more than one element. The first is the result of a successful match, followed by the result of a successful match in parentheses, and if there are multiple parentheses, the result of their matching success will be the array element

<! DOCTYPE html>
 
 

The array returned after calling the Exec method has the following two properties:

Input the entire original to match the string
Index the entire pattern matches the starting position of the success
Methods that support String objects of regular expressions

Search method

The search () method retrieves the substring specified in the string, or retrieves a substring that matches the regular expression.

Return value: The starting position of the first substring in stringobject that matches the regexp.

Note: If no matching substring is found, return-1.

The search () method does not perform a global match, and it ignores flag g. It ignores the RegExp lastindex property at the same time and always retrieves it from the beginning of the string, which means it always returns the first matching position of Stringobject.

Demo:

<! DOCTYPE html>
 
 

Match method

The match () method retrieves the specified value within a string, or finds a match for one or more regular expressions. The method is similar to IndexOf () and LastIndexOf (), but it returns the specified value, not the position of the string.

The match method for string objects is similar to the Exec method of a regular object:

However, if the regular expression has a G modifier, then the match method differs from the Exec method:

You can see that match returns all the results of a successful match, but the Exec method only returns one.
Demo:

<! DOCTYPE html>
 
 

Replace method

The replace () method replaces some characters in a string with some other characters, or replaces a substring that matches a regular expression.

Return value: A new string that is replacement replaced with the first match of RegExp or all matches.

The replace () method of the string Stringobject performs a find-and-replace operation. It will look for substrings in the stringobject that match the regexp, and then replace them with replacement. If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matching substring.

Demo:

<! DOCTYPE html>
 
 

Special character substitution in replace method

Demo:

 <! DOCTYPE html>  
 

The parameter replacement of replace is a function

Match matches the entire string, that is: xyz45678%$&^
A1 is the first subexpression, ([^\d]*), that matches 0 or more non-numeric characters, namely: XYZ
A2 is the second subexpression, (\d*), that matches 0 or more digits, namely: 45678
A3 is the third subexpression, ([^\w]*), matching 0 or any non word characters. equivalent to [^a-za-z0-9_], i.e.%$&^
Index is the location where the pattern match appears, from the first character has been matched successfully, then the position is 0
String is the strings themselves, that is, the xyz45678%$&^

Demo:

<! DOCTYPE html>
 
 

Split method

Split (' String split regular ', ' returns the maximum number of members of the array '); Returns an array of the divided parts
Demo:

<! DOCTYPE html>
 
 

You can transform a regular matching rule to split a string.

The following regular matching rule is split with 0 or more x, and if the parentheses match the parentheses, the split rule is returned as an array member.

Demo2:

<! DOCTYPE html>
 
 

Some applications of regular expressions

1. Characters with the most occurrences in the string

var re =/(\w) \1+/g;
The parentheses outside (\w) represent groupings, \1 indicate that the contents of the first group are repeated, \1+ the characters that \w matches to repeat n times, and the following G indicates that all replacements are performed

The second parameter of the str.replace is a function, and parameter a represents the entire matched string, and B means that the first capture grouping is a single character that appears duplicated, comparing a.length with the most repeated num that has been recorded, and assigning it to num if the a.length is larger. Repeat character B with value, this function returns the replacement text, but there is no return value, that is, replace with NULL, each replacement function is executed

<! DOCTYPE html>
 
 

2. Extract the subdomain from the URL

<! DOCTYPE html>
 
 

3. Add a wildcard to a string

<! DOCTYPE html>
 
 

Regular expressions that are commonly used

Match domestic phone number: \d{3}-\d{8}|\d{4}-\d{7}

such as 0511-4405222 or 021-87888822

Match QQ number: [1-9][0-9]{4,}

Starting from 10000

Zip code: [1-9]\d{5} (?! \d)

Postal code is 6 digits

Matching ID:/^ (\d{14}|\d{17}) (\d|[ XX]) $/

Matching rule: ID number 15 digits or 18 digits, the last one may be x, the other is all digital

Matching IP address: \d+.\d+.\d+.\d+

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

Matching Chinese characters:/[\u4e00-\u9fa5\uf900-\ufa2d]/

With Unicode, you must start with \u, followed by a four-bit 16-digit representation of the character encoding

Matching Email Address:

/^ ([a-za-z_0-9-]) +@ ([a-za-z_0-9-]) + (. [ A-za-z_0-9-]) +$/

The rule of the mailbox is: Made up of 3 parts

By one or more alphanumeric underscores and bars + @ + 1 or more alphanumeric underscores and staves +. + 1 or more alphanumeric underscores and bars

Match URL url:[a-za-z]+://[^\s]*

Determine if a string is made up of numbers:/^\d*$/

Limit text boxes can only enter numbers and decimal points (two-bit decimal point):

/^\d*.? \d{0,2}$/

Note: There are 0 or more digits at the beginning, (? represents 0 or more times before the match) with 0 or 1 decimal points, followed by 0 or up to 2 digits

User name Regular:/^[\u4e00-\u9fa5\uf900-\ufa2d\w]{4,16}$/

Matching rules: Only Chinese, English, numerals, underscores, 4-16 characters

Matching Chinese text single-character:/[\u4e00-\u9fa5\uf900-\ufa2d]/

\w is matching English, numerals, underscores

Match English address:/^[a-za-z][.a-za-z\s,0-9]*? [a-za-z]+/matching rule: Contains points, letters, spaces, commas, numbers, but the beginning and end must be alphabetic analysis: The beginning must be a letter to write the/[a−za−z]/end must be written as a letter:/[a−za−z]+/

Middle contains dots, letters, spaces, commas, numbers of regular:/[.a-za-z\s,0-9]*?/

The number on the outside is 0 or more, followed by a question mark? The representative is dispensable; there is a match, there is no match;

Matching Price:/^\d* (. \d{0,2})? $/

Match rule: There are 0 or more digits at the beginning, there may be a decimal number in the middle, and there may be 0-2 decimal digits behind it.

First letter of Word:/\b (\w) |\s (\w)/g

Validate Date format:/^\d{4}[-\/]\d{1,2}[-\/]\d{1,2}$/

There are 2 types of date formats, the first of which is YYYY-MM-DD or YYYY/MM/DD.

Analysis: Months and days can have 1-or 2-bit

The above is small series for everyone to bring the JS regular expression Learning and summary (must see article) all content, I hope that we support the cloud-Habitat Community ~

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.