JS Regular expression meta-character

Source: Internet
Author: User
Tags modifiers

G is a pattern modifier that indicates multiple lookups in the entire string.

Match method to find content that matches the reg regular match

"AB23839CD". Match (/\d+/)

Blank \s (Including Space return tab, etc.)

\f matches the page break, \ n matches the newline character, \ r matches the carriage return, \ t matches the tab, and the \v matches the Vertical tab.

\s matches a single space, equivalent to [\f\n\r\t\v]. For example:

Example 1:

var reg =/\s.+/;

var str= ' This is a test String. ';

Alert (reg.exec (str));

Returns "is a test String.", the regular means matches the first space and any subsequent non-newline characters.

Similarly, \s represents a non-whitespace character.

Example 2:

var reg =/\s+/;

var str= ' This is a test String. ';

Alert (reg.exec (str));

The match result is this, and when the first space is encountered, the regular stops matching.

One of the most commonly used in regular is:

Example 3:var reg=/^\s*$/; Match any empty or white space character, if you do not enter anything, or enter only spaces, carriage returns, newline characters, then the match succeeds. This allows you to verify that the user entered the content correctly.

This is used to verify that valid characters are written in the input box, using the following:

var reg=/^\s*$/;

if (reg.test (value)) {

Alert (' Please enter a valid value ');

return false;

}

Single-character \w

\w represents a word character, equivalent to the character set [a-za-z0-9_]. For example:

var reg =/\w+/;

var str= ' Zhufengpeixun ';

Alert (reg.exec (str));

Returns the full Zhufengpeixun string, because all characters are word characters.

var reg =/\w+/;

var str= '. ClassName ';

Alert (reg.exec (str));

The results display matches the classname in the string, with only the first "." The unique non-word character does not match.

var reg =/\w+/;

var str= ' regular tutorial ';

Alert (reg.exec (str));

Attempting to match Chinese characters with a word character does not work and returns NULL.

\w represents a non-word character, equivalent to [^a-za-z0-9_]

var reg =/\w+/;

var str= ' regular tutorial ';

Alert (reg.exec (str));

Returns the complete string because Chinese is counted as a non-word character.

Grouping and grouping references

See the regular topic section of the online video

Regular Expression Basics Third: grouping, grouping of references, selection, etc. http://online.zhufengpeixun.cn/viewCourseDetail.do?courseId=141415

The form is as follows:/(Zhong expression) \1/is still illustrated with an example:

1. Example

var reg =/\w/;

var str= ' Zhufengpeixun ';

Alert (reg.exec (str));

Returns Z.

2. Example

var reg =/(\w) (\w)/;

var str= ' Zhufengpeixun ';

Alert (reg.exec (str));

Return zh,z,h, ZH is the entire regular match content, Z is the first parenthesis in the Zhong expression matches the content, H is the second parenthesis matches the content.

3. Example

var reg =/(\w) \1/;

var str= ' Zhufengpeixun ';

Alert (reg.exec (str));

NULL is returned. The "\1" here is called a reverse reference, which represents the content of the Zhong expression in the first parenthesis. In the above example, the first parenthesis (\w) matches Z, so "\1" is the same as Z, and in the rest of the string there is no way to find Z. In contrast to the second example, it can be found that "\1" is equivalent to "1th bracket matching" instead of "the contents of the first parenthesis".

var reg =/(\w) \1/;

var str= ' bbs.zhufengpeixun.cn ';

Alert (reg.exec (str));

This regular will be matched to the bb,b. Similarly, there are several sub-regular expressions in front of which we can use several reverse references. For example:

var reg =/(\w) (\w) \2\1/;

var str= ' woow ';

Alert (reg.exec (str));

Matches successfully, because the first bracket matches to W, the second bracket matches to O, and \2\1 represents OW, exactly matching the last two characters of the string.

Parentheses (), representing sub-expressions, also called grouping

Before we discussed the question of parentheses, see the following example:

var reg =/^ (b|c). +/;

var str= ' bbs.blueidea.com ';

Alert (reg.exec (str));

This is to implement a string that matches only the strings beginning with B or C, and matches the newline character, but. As we've seen above, you can use "\1" to reverse-reference the Zhong in parentheses that match the expression. The Exec method also saves the matching result of the word regular expression to the returned result.

Do not record matching results for child regular expressions [match not caught]

Using a regular shape like (?:p Attern) avoids saving the matching results in parentheses. For example:

var reg =/^ (?: b|c). +/;

var str= ' bbs.blueidea.com ';

Alert (reg.exec (str));

You can see that the returned results no longer include the words in the parentheses that match the multiple matching contents. Similarly, the reverse reference is not good enough:

var reg =/^ (b|c) \1/;

var str= ' bbs.zhufengpeixun.cn ';

Alert (reg.exec (str));

Returns BB,B. BB is the content that matches the entire regular expression, and B is the first Zhong that matches the expression.

var reg =/^ (?: b|c) \1/;

var str= ' bbs.zhufengpeixun.cn ';

Alert (reg.exec (str));

Returns NULL. There is no way to reverse reference because there is no record of what is matched in parentheses.

Forward Pre-check

Form: (? =pattern) The so-called forward pre-check, meaning: to match the string, must follow the pattern! We know that the regular expression/cainiao/will match the Cainiao. Similarly, the Cainiao in Cainiao9 is also matched. But we may hope that Cainiao can only match the Cainiao in Cainiao8. This time you can write as follows:/cainiao (? =8)/, see two examples:

var reg =/cainiao (? =8)/;

var str= ' cainiao9 ';

Alert (reg.exec (str));

Returns NULL.

var reg =/cainiao (? =8)/;

var str= ' Cainiao8 ';

Alert (reg.exec (str));

Match Cainiao. It is important to note that the bracketed content does not participate in the true match, but only checks to see if the following characters meet the requirements, such as the above regular, returning Cainiao, not Cainiao8.

Let's look at a few more examples:

var reg =/zhufeng (? =peixun)/;

var str= ' Zhufengpeixun ';

Alert (reg.exec (str));

Match to Zhufeng, not Peixun.

var reg =/zhufeng (? =peixun)/;

var str= ' Zhufengonline ';

Alert (reg.exec (str));

Returns NULL because Zhufeng is not a peixun behind.

var reg =/zhufeng (? =peixun)/;

var str= ' Onlinepeixun ';

Alert (reg.exec (str));

also returns NULL.

?!

Form (?! pattern) and? = exactly the opposite, requires that the string should not be followed by a certain pattern, but also take the example above:

var reg =/zhufeng (?! JS)/;

var str= ' Zhufengjs ';

Alert (reg.exec (str));

Return NULL, because the regular request, Zhufeng can not be after the JS.

var reg =/zhufeng (?! JS)/;

var str= ' Zhufengpeixun ';

Alert (reg.exec (str));

The Zhufeng is returned successfully.

Match Meta characters

The first thing to figure out is what is a meta-character? We've used *,+,? And the like, and they have a certain special meaning in regular expressions, similar to characters that have special functions called metacharacters. For example

var reg =/c*/;

There is an arbitrary C, but what if we really want to match the c* string? As long as the * escaped, you can, as follows:

var reg =/c\*/;

var str= ' c* ';

Alert (reg.exec (str));

Returns a matching string: c*.

Similarly, to match other metacharacters, just add a "\" to the front.

Modifiers for regular expressions

Global match, Modifier g

Form:/pattern/g Example: Reg =/b/g; The function of this g is again in the back. Let's look at the two modifiers in the back. Case insensitive, modifier i

Form:/pattern/i Example:

var reg =/b/;

var str = ' BBS ';

Alert (reg.exec (str));

Returns NULL because the casing does not match.

var reg =/b/i;

var str = ' BBS ';

Alert (reg.exec (str));

Match to B, this is the I modifier.

Beginning line end, modifier m

Form: The function of the/pattern/m m modifier is to modify the function of ^ and $ in regular expressions so that they represent the beginning and end of the line, respectively. For example:

var reg =/^b/;

var str = ' Test\nbbs ';

Alert (reg.exec (str));

The match failed because there is no B character at the beginning of the string. But after adding the M modifier:

var reg =/^b/m;

var str = ' Test\nbbs ';

Alert (reg.exec (str));;

Match to B, because after adding the M modifier, ^ has already represented the beginning of the line, because the BBS at the beginning of the second row of the string, it can be successfully matched.

Matches a fixed n-c{n}

{1} indicates a meaning. /c{1}/can only match a C, and/c/is a meaning, generally match only one occurrence of the character, the back of {1} will not be written. The/c{2}/will match two consecutive c. /c{n}/, in other words, matches n consecutive c. Look at the following example:

var reg =/c{1}/;

var str= ' China_zhufengpeixun ';

Alert (reg.exec (str));

The output is: C

var reg =/o{2}/;

var str= ' money ';

Alert (reg.exec (str));

Returns the result ' null ', indicating that no match was successful.

reg =/o{2}/;

Str= ' Good food ';

Alert (reg.exec (str))

Outputs the result oo. (In fact, the first set of OO, will not be matched to the second set of OO, because the regular match is lazy, without the pattern match g, it means only to match once, the match is returned and stopped.)

If written

reg=/o{2}/g;

Alert (Str.match (REG))

The output is Oo,oo.

C{m,n} matches a minimum of M, up to N

c{3,4} means 3 consecutive C or 4 C. For example:

reg =/o{3,4}/;//(matches three to four O)

Str= ' good regular tutorial ';

Alert (reg.exec (str));

Returns the result null, indicating that no match was successful. Cases:

reg =/o{3,4}/;

Str= ' goood regular tutorial ';

Alert (reg.exec (str));

The result of the popup is: ooo. Cases:

reg =/o{3,4}/;

Str= ' very gooood regular tutorial ';

Alert (reg.exec (str));

The result of the output is: Oooo, which indicates that the regular will try to match as much as possible, 3 can be 4 when it chooses to match one more. (This is the greedy match) example:

reg =/c{3,4}/;

Str= ' ccccctest ';

Alert (reg.exec (str));

will still match 4 C.

As can be inferred from the above example: C{m,n} represents m to n C, and M is less than or equal to N.

C{n,} means a minimum match of N C, up to No limit

C{1,} represents more than 1 C, equivalent to +. As follows:

Cases:

reg =/c{1,}/;str= ' Cainiao ';

Alert (reg.exec (str));

The result pops up C.

Cases:

reg =/c{1,}/;

Str= ' ccccctest ';

Alert (reg.exec (str));

Return to CCCCC, again stating that the regular expression will match as much as possible.

Cases:

reg =/c{2,}/;

Str= ' Cainiao ';

Alert (reg.exec (str));

The result returns NULL,C{2,} represents more than 2 C, while Cainiao only has 1 c.

From the above example, C{n,} represents a minimum of n C, up to an unlimited number.

Synthesis: *,+,?

* indicates 0 or more times, equivalent to {0,}, i.e. c* and c{0,} is a meaning.

+ means one or more times, equivalent to {1,}, i.e. c+ and C{1,} is a meaning.

Last, 0 or 1 times, equivalent to {0,1}, C? and c{0,1} is a meaning.

Greedy and non-greedy "greedy match and non-greedy match"

Men are greedy, and so are they. We have seen in example reg =/c{3,4}/;str= ' Cccctest ', and can match four times, and it will definitely not match three. All of the above mentioned are like this, as long as the legal situation, they will try to match the characters as much as possible, this is called greedy mode. If we want the regular to match as few characters as possible, then we can add one after the symbol that represents the number (that is, the question mark is appended to the quantifier, which indicates a non-greedy match). The following forms are composed:

{n,}?, *?, +?,??, {m,n}?

Cases:

reg =/c{1,}?/;

Str= ' CCCCC ';

Alert (reg.exec (str));

The returned result is only 1 C, although 5 C can match, but because the regular expression is a non-greedy pattern, it will only match one.

/^ opening, ending $/"representing position"

^ indicates that only the beginning of the string is matched. Look at the following example:

Example 1:

reg =/^c/;

Str= ' Vitamin C ';

Alert (reg.exec (str));

The result is null because the string ' vitamin C ' begins with not C, so the match fails.

Example 2:

reg =/^z/;

Str= ' Zhufengpeixun ';

Alert (reg.exec (str));;

This time return C, the match succeeds, because the Cainiao exactly begins with Z.

In contrast, $ then matches only the character at the end of the string, again, see Example:

Example 3:

reg =/z$/;

Str= ' Zhufengpeixun ';

Alert (reg.exec (str));

The output is null, indicating that the regular expression failed to find the z character at the end of the string.

Example 4:

reg =/d$/;

Str= ' regular tutorial good ';

Alert (reg.exec (str));

The result of this return is D, which indicates a successful match.

Meta-character dot '. ' Usage of

. Matches all characters in the string except for the newline character, such as

reg =/./; A point represents the first non-newline character that appears in the matching string.

Str= ' Zhufengpeixun ';

Alert (reg.exec (str));;

The results show that the regular matches to the character Z.

reg =/./;

Str= ' Online.zhufengpeixun ';

Alert (reg.exec (str));

This time is O, as long as there is a non-newline character, it means that the match is successful, it will not go down again.

reg =/.+/;

Str= ' Zhufengpeixun_ Front-end Development Authority training ';

Alert (reg.exec (str));

The result is "Zhufengpeixun_ front-end development" which means that all characters are matched, including a space, a glide line

"Greedy Match".

Example 1:

reg =/.+/;

Str= ' online.zhufengpeixun.cn ';

Alert (reg.exec (str));;

Similarly, return the entire string--online.zhufengpeixun.cn directly, visible "." Also matches the "." Itself.

Example 2:

reg =/^./; This means that you must start with a non-newline character.

Str= ' \nzhufengpeixun ';

Alert (reg.exec (str));

The result is null, which finally fails, and the regular requires that the first character of the string is not a newline, but the exact character begins with \ n.

"|", in the regular expression or, put "|" One or more characters of the left and right sides treated as a whole

B|c, Match B or C (this is equivalent to [BC]). Ab|ac is a match for AB or AC (but this is not equivalent to [abc],[] for any one of a set of characters).

Example 1:

/z|o/.exec (' Zhufengpeixun '))

The result is Z.

Example 2:

/z|o/.exec (' online ');

The result is O.

Example 3:

/^z|o.+/.exec (' online ');

Match the entire online.

Example 4:

/^z|o.+/.exec (' zhufengpeixun.cn ');

The result is only one z, not the entire string. Because the regular expression above means, match the beginning of the z or the o.+.

Combined with parentheses

Cases:

/^ (Z|o). +/.exec (' Zhufengpeixun ');

This time the result is the entire string Zhufengpeixun, plus the parentheses above this, this regular means that if the beginning of the string is Z or O, then match the beginning of Z or O and all the non-newline characters thereafter. If you experiment, you will find that the result of the return is a "Z", which is the content of the Z|o (this is called grouping or sub-regular). What we write in parentheses inside the regular expression is considered a Zhong expression, and the matching results are recorded for later use. We're going to ignore this feature for the moment.

Effect of square brackets: []

[ABC] represents either a or B or any one of the characters in C.

Cases:

var reg =/^[abc]/;

This is a bit like/^ (A|B|C)/

Str= ' bbs.zhufengpeixun.cn ';

Alert (reg.exec (str));;

The return result is B.

Cases:

reg =/^[abc]/;

str= ' test ';

Alert (reg.exec (str));

The result of this time is null.

We use the following notation in the character set: [A-z],[a-z],[0-9], which represents lowercase letters, uppercase letters, and numbers, respectively. For example:

reg =/^[a-za-z][a-za-z0-9_]+/;

This is actually the meaning of the meta-character \w representation.

str= ' test ';

Alert (reg.exec (str));

The result is the whole test, which means that the beginning must be an English letter, followed by an English letter or a number and an underscore.

About consecutive characters in a regular

In the regular/[0-9]/represents any one of the matching characters from 0 to 9,/[a-z]/means that any one letter from A to Z can be used as long as there are consecutive characters in the ASCII code table.

Please Baidu "ASCII code table", referring to the order in which the characters appear and corresponding 16 or 10 binary encoding.

such as Var reg=/^[!-z]$/; It will match from the character "!" Begins with any character that ends with the character "Z".

var reg=/^[!-z]$/;

Alert (Reg.test ("8"));//true,

Alert (Reg.test ("*"))//true,

Alert (Reg.test ("}"))//false, because "}" is not from! Within this range of Z

Regular/^[!-z]$/can also be represented by the 16 binary. If you are using a 16 binary representation, you need to start with \u, which means that the Unicode character is defined in 16 notation, and the code behind the 16 binary is written as four bits, and 4 is less than 0 bits in front of it. Then the top of the regular, can also be written

var reg=/^[\u0021-\u007a]$/;

Character "! "The corresponding 16 encoding is 21, the character" Z "of the 16 encoding is 7a.

This way. Chinese is an extended ASCII character encoding, and the regular match for UTF8 Chinese is:/^[\u4e00-\u9fa5]+$/

The regular expression matching Chinese characters in PHP with Utf-8 encoding is:/^[\x{4e00}-\x{9fa5}]+$/u

Note: written as [1-13] is not represented from the number 1 to the number 13, but rather from 1 to 1 and 3, that is, 1 and 3. Because the regular is in the expression of consecutive occurrences of the character, rather than the number.

Anti-character set [^ABC]

^ At the beginning of the regular expression the meaning of the beginning, for example,/^c/is the beginning of C, but in the character set and, it represents a similar "non" meaning, for example [^ABC] means that can not be either a, B or any of C. For example:

var reg =/[^abc]/;

var str= ' Blueidea ';

Alert (reg.exec (str));

The result returned is L because it is the first non-ABC character (that is, the first B does not match). Same:

Cases:

var reg =/[^abc]/;

var str= ' Cbazhufengpeixun ';

Alert (reg.exec (str));

Output Z, the first three characters are all in the [ABC] collection. From this we know: [^0-9] means not a number, [^a-z] represents a non-lowercase letter, and so on.

Boundary and non-boundary

\b Represents the meaning of the boundary, that is, only the beginning and end of the string are counted. For example,/\bc/represents the beginning of the string C. Look at the following example:

/\bc/.exec (' Cainiao ');

Returns the result of C. Matches the C character to the left border.

/\bc/.exec ('??? C ');

Still returns C, but this time returns the right border of C.

/\bc/.exec (' BCB ');

This match failed because C in the BCB string is sandwiched between the left and right boundaries.

The corresponding \b with \b represents non-boundary. For example:

/\bc/.exec (' BCB ');

This time it will be successfully matched to the C in BCB. However

/\bc/.exec (' Cainiao ');

NULL is returned. Because \b tells the regular, only the non-bounding c is matched.

Digital and non-digital

\d denotes the meaning of a number, whereas \d represents a non-number.

Cases:

/\d/.exec (' Cainiao8 ')

The matching result returned is 8 because it is the first numeric character.

Cases:

/\d/.exec (' Cainiao8 ');

Returns C, the first non-numeric character.

Turn from: 1190000002471140

Sutaking

JS Regular expression meta-character

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.