JS Regular Expression

Source: Internet
Author: User
Tags alphabetic character

Symbol
A regular expression consists of a set of ordinary characters and metacharacters.
Ordinary characters include numbers and uppercase and lowercase letters.

Meta-character Description
\ marks the next character as a special character, or a literal character, or a backward reference, or an octal escape character. For example, "\\n" matches \ n. "\ n" matches the line break. The sequence "\ \" matches "\" and "\ (" Matches "(".
^ matches the starting position of the input string. If the multiline property of the RegExp object is set, ^ also matches the position after "\ n" or "\ r".
$ matches the end position of the input string. If the multiline property of the RegExp object is set, $ also matches the position before "\ n" or "\ r".
* matches the preceding subexpression 0 or more times (greater than or equal to 0 times). For example, zo* can match "Z", "Zo" and "Zoo". * Equivalent to {0,}.
+ matches the preceding subexpression one or more times (greater than or equal to 1 times). For example, "zo+" can Match "Zo" and "Zoo", but not "Z". + equivalent to {1,}.
Match the preceding subexpression 0 or one time. For example, "Do (es)?" You can match "do" in "does" or "does".? = {0,1}.
{N}n is a non-negative integer.} Matches the determined n times. For example, "o{2}" cannot match "O" in "Bob", but can match two o in "food".
{N,}n is a non-negative integer.} Match at least n times. For example, "o{2,}" cannot match "O" in "Bob", but can match all o in "Foooood". "O{1,}" is equivalent to "o+". "O{0,}" is equivalent to "o*".
{N,M}M and N are non-negative integers, where n<=m. Matches at least n times and matches up to M times. For example, "o{1,3}" will match the first three o in "Fooooood". "o{0,1}" is equivalent to "O?". Note that there can be no spaces between a comma and two numbers.
? When the character immediately follows any other restriction (*,+,?,{n},{n,},{n,m}), the matching pattern is non-greedy. The non-greedy pattern matches the searched string as little as possible, while the default greedy pattern matches as many of the searched strings as possible. For example, for the string "Oooo", "o+?" A single "O" will be matched, and "o+" will match all "O".
. Point matches any single character except "\ r \ n". To match any character that includes "\ r \ n", use a pattern like "[\s\s]".
Pattern matches the pattern and gets the match. The obtained matches can be obtained from the resulting matches collection, the Submatches collection is used in VBScript, and the $0...$9 property is used in JScript. To match the parentheses character, use "\ (" or "\").
(?:p Attern) matches the pattern but does not get a matching result, which means that this is a non-fetch match and is not stored for later use. This is used in the or character "(|)" It is useful to combine the various parts of a pattern. For example, "Industr (?: y|ies)" is a more abbreviated expression than "industry|industries".
(? =pattern) is positively pre-checked, matching the lookup string at the beginning of any string that matches the pattern. This is a non-fetch match, which means that the match does not need to be acquired for later use. For example, "Windows (? =95|98| nt|2000) "Can match" windows "in" Windows2000 ", but does not match" windows "in" Windows3.1 ". Pre-checking does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, rather than starting with the character that contains the pre-check.
(?! pattern) forward negation, matching the search string at the beginning of any mismatch pattern. This is a non-fetch match, which means that the match does not need to be acquired for later use. For example, "Windows (?! 95|98| nt|2000) "Can match" windows "in" Windows3.1 ", but does not match" windows "in" Windows2000 ".
(<=pattern) Reverse positive pre-check, similar to positive pre-check, just the opposite direction. For example, "(? <=95|98| nt|2000) Windows can match "Windows" in 2000Windows, but not "windows" in "3.1Windows".
Reverse negation (<!pattern) is similar to positive negative pre-check, except in the opposite direction. For example "(? <!95|98| nt|2000) Windows can match "Windows" in 3.1Windows, but not "windows" in "2000Windows".
X|y matches x or Y. For example, "Z|food" can match "Z" or "food". "(z|f) Ood" matches "Zood" or "food".
[XYZ] Character set. Matches any one of the characters contained. For example, "[ABC]" can Match "a" in "plain".
[^XYZ] negative character set. Matches any character that is not contained. For example, "[^ABC]" can match "Plin" in "plain".
A [A-z] character range. Matches any character within the specified range. For example, "[A-z]" can match any lowercase alphabetic character in the range "a" to "Z".
Note: The range of characters can be represented only if the hyphen is inside a character group and is between two characters; If the beginning of the character group is out, only the hyphen itself can be represented.
[^a-z] negative character range. Matches any character that is not in the specified range. For example, "[^a-z]" can match any character that is not in the range "a" to "Z".
\b Matches a word boundary, which is the position between a word and a space. For example, "er\b" can Match "er" in "never", but cannot match "er" in "verb".
\b Matches a non-word boundary. "er\b" can Match "er" in "verb", but cannot match "er" in "Never".
\CX matches the control character indicated by X. For example, \cm matches a control-m or carriage return. The value of x must be one of a-Z or a-Z. Otherwise, c is considered to be a literal "C" character.
\d matches a numeric character. equivalent to [0-9].
\d matches a non-numeric character. equivalent to [^0-9].
\f matches a page break. Equivalent to \x0c and \CL.
\ n matches a line break. Equivalent to \x0a and \CJ.
\ r matches a carriage return character. Equivalent to \x0d and \cm.
\s matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].
\s matches any non-whitespace character. equivalent to [^ \f\n\r\t\v].
\ t matches a tab character. Equivalent to \x09 and \ci.
\v matches a vertical tab. Equivalent to \x0b and \ck.
\w matches any word character that includes an underscore. Equivalent to "[a-za-z0-9_]".
\w matches any non-word character. Equivalent to "[^a-za-z0-9_]".
\XN matches N, where n is the hexadecimal escape value. The hexadecimal escape value must be two digits long for a determination. For example, "\x41" matches "A". "\x041" is equivalent to "\x04&1". ASCII encoding can be used in regular expressions.
\num matches num, where num is a positive integer. A reference to the obtained match. For example, "(.) \1 "matches two consecutive identical characters.
\ n identifies an octal escape value or a backward reference. n is a backward reference if \ n is preceded by at least one of the sub-expressions obtained. Otherwise, if n is the octal number (0-7), N is an octal escape value.
\NM identifies an octal escape value or a backward reference. If at least NM has obtained a subexpression before \nm, then NM is a backward reference. If there are at least N fetches before \nm, then n is a backward reference followed by the literal m. If none of the preceding conditions are met, if both N and M are octal digits (0-7), then \nm will match the octal escape value nm.
\NML if n is an octal number (0-7) and both M and L are octal digits (0-7), the octal escape value NML is matched.
\un matches N, where N is a Unicode character represented by four hexadecimal digits. For example, \u00a9 matches the copyright symbol (&copy;).
\< \> the Start (\<) and End (\>) of the matching word (word). For example, the regular expression \<the\> can match "the" in the string "for the wise", but cannot match "the" in the string "otherwise". Note: This meta-character is not supported by all software.
\ (\) defines the expression between \ (and \) as a group, and saves the characters that match the expression to a staging area (up to 9 in a regular expression), which can be referenced using \1 to \9 symbols.
| A logical OR (or) operation of two matching criteria. For example, the regular expression (Him|her) matches "It belongs to him" and "it belongs to her", but does not match "it belongs to them." Note: This meta-character is not supported by all software.
+ match 1 or more of that character just before it. For example, the regular expression + + matches 9, 99, 999, and so on. Note: This meta-character is not supported by all software.
Match 0 or 1 of that character just before it. Note: This meta-character is not supported by all software.
{i} {i,j} matches a specified number of characters, which are defined in an expression preceding it. For example, the regular expression a[0-9]{3} can match the character "A" followed by a string of exactly 3 numeric characters, such as A123, A348, etc., but does not match A1234. The regular expression [0-9] {4,6} matches any 4, 5, or 6 digits in a row.

The simplest metacharacters is the point, which can match any single character (note not including line breaks).

For example:
1. Change the instance of all method Foo (a,b,c) to Foo (b,a,c). Here A, B, and C can be any arguments provided to the method foo ().
[^,] any character other than a comma
[^,]* 0 or more non-comma characters
\ ([^,]*\) marks these non-comma characters as \1 so that it can be referenced in a later substitution pattern expression, \ (\) to define the group.
\ ([^,]*\), we must find 0 or more non-comma characters followed by a comma, and the non-comma character part to be marked for later use.
This expression we have already analyzed: foo ([^,]*\], this paragraph can be simply translated as "When you find Foo (just before the first comma before the section marked as \1". Then we use the same method to mark the second parameter as \2. The same is true for the third parameter, except that we want to search all characters until the closing parenthesis. We don't have to search for the third parameter, because we don't need to adjust its position, but this pattern guarantees that we're only going to replace the Foo () method call with three parameters, which is often more secure when Foo () is an overloaded (overloading) method. Then, in the replacement section, we find the corresponding instance of Foo () and then replace it with the marked part, which is the exchange position of the first and second parameters.
Foo (\ ([^,]* \), \ ([^,]* \), \ ([^]* \)]
Foo (\2,\1,\3)

Shorthand Comprehension Skills
. [] ^ $ four characters are regular expressions supported by all languages, so this four is the underlying regular expression.

Equivalent:
? Equivalent to match length {0,1}
* Equivalent to match length {0,}
+ equivalent to match length {1,}
\d equivalent to [0-9]
\w is equivalent to [a-za-z_0-9].

Common operators and expressions:
^ Start
() domain segment
[] contains, default is a character length
[^] not included, default is a character length
{n,m} match length
. Any single character (\. Character point)
| Or
\ Escape
$ end
[A-z] 26 uppercase letters
[A-z] 26 lowercase letters
[0-9] 0 to 9 digits
[A-za-z0-9] 26 caps, 26 lowercase letters, and 0 to 9 digits
Segmentation

Split syntax:
[A,h,t,w] contains A or H or T or W letters
[0,3,6,8] contains 0 or 3 or 6 or 8 digits

Grammar and Interpretation:
Basic Syntax "^ ([]{}) ([]{}) ([]{}) $"
Regular string = "Start ([contains content]{length}" ([Contains content]{length}) ([Contains content]{length}) End "

Instance:
1. String: tel:086-0666-88810009999
Regular matches: ^tel:\d{1,3}-[0]\d{3}-\d{11}$

2. Replace the specified content to the end of the line
Original text: 12313 ABC Weouorwe
To be replaced by: replace ABC and subsequent characters with ABC DEFG
Str.replace (/abc.*/, "ABC DEFG")

3. Digital replacement
Original text: ASDADAS123ASDASDAS456ASDASDASD789ASDASD
To be replaced by: ASDADAS[123]ASDASDAS[456]ASDASDASD[789]ASDASD
Str.replace (/\d+/g, function (word) {return ' [' +word + '] '})

4. Delete the specified character at the end of each line
Original text: 12345 1265345
Delete the end of the 345
Str.replace (/345$/, "")

5. Verify your phone number: ^\d{3,4}-\d{7,8}$
Format: xxx/xxxx-xxxxxxx/xxxxxxxx

6. Enter only the number: ^\d*$

7. Only n digits can be entered: ^\d{n}$

8. You can only enter numbers with at least n digits: ^\d{n,}$

10. Enter only m~n digits: ^\d{m,n}$

11. Only positive real numbers with two decimal places can be entered: ^[0-9]+\. [0-9] {2}$

12. Get date Regular expression: ^\d{4}[year |\-|\.] +\d{1,2}[month |\-|\.] +\d{1,2}[Day]?$
Format: 2014-06-12 June 12, 2014 2014.9.12

Source: Http://baike.baidu.com/view/94238.htm?fr=aladdin
Test tool: Http://tool.oschina.net/regex

Source: http://www.cnblogs.com/zourong/p/3897013.html

JS Regular Expression

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.