JavaScript regular expressions related to the use of a detailed approach

Source: Internet
Author: User
Tags alphabetic character

One, to use the regular expression related method, first must understand the regular expression basic usage, excerpt from Baidu Encyclopedia:

Metacharacters Describe
\ Marks the next character as a special character, or a literal character, or a backward reference, or a octal escape character. For example, "\\n" matches \ n. "\ n" matches line breaks. The sequence "\ \" matches "\" and "\ (matches" ().
^ Matches the start 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, the $ 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". * is 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 cannot match "Z". + is equivalent to {1,}.
? Match the preceding subexpression 0 times or once. For example, "Do (es)?" You can match the "do" in "do" or "does".
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, of which n<=m. Matches n times at least 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?". Notice that there is no space between the comma and the two number.
? When the character is immediately following any other qualifier (*,+,?,{n},{n,},{n,m}), the match pattern is not greedy. Non-greedy patterns match as few strings 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 including "\ r \ n", use a pattern like "[\s\s]".
(pattern) Match pattern and get this match. The obtained matches can be obtained from the resulting matches collection, use the Submatches collection in VBScript, and use the $0...$9 property in JScript.   To match the parentheses character, use "\ (" or "\)". Matches in parentheses are preserved
(?:p Attern) Matches pattern but does not get a matching result, which means it is a non fetch match and is not stored for later use. This is in use or the character "(|)" It is useful to combine parts of a pattern.  For example, "Industr (?: y|ies)" is an expression more abbreviated than "Industry|industries". Clear matching of parentheses
(? =pattern) Forward positive check, match the lookup string at the beginning of any string matching pattern. This is a non-fetch match, that is, the match does not need to be acquired for later use. For example, the Windows (? =95|98| nt|2000) "Can match windows in Windows2000, but cannot match windows in Windows3.1." It does not consume characters, that is, after a match occurs, the next matching search begins immediately after the last match, instead of starting after the character that contains the pre-check.
(?! Pattern Forward negation, which matches the lookup string at the beginning of any string that does not match the pattern. This is a non-fetch match, that is, 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 cannot match windows in Windows2000."
(? <=pattern) The reverse positive check is similar to positive, but in the opposite direction. For example, "(? <=95|98| nt|2000) Windows can match "Windows" in "2000Windows", but it does not match "windows" in "3.1Windows".
(? <!pattern) Reverse negation is similar to positive negation, except in the opposite direction. For example, "(? <!95|98| nt|2000) Windows can match "Windows" in "3.1Windows", but it does not match "windows" in "2000Windows".
X|y Match x or Y. For example, "Z|food" can match "Z" or "food". "(z|f) Ood" matches "Zood" or "food".
[XYZ] Character set combination. Matches any one of the characters contained. For example, "[ABC]" can Match "a" in "plain".
[^XYZ] Negative character set combination. Matches any characters that are not included. For example, "[^ABC]" can match "Plin" in "plain".
[A-z] The range of characters. Matches any character within the specified range. For example, "[A-z]" can match any lowercase alphabetic character in the range "a" through "Z". Note: The range of characters can only be represented when the hyphen is inside the character group and occurs between two characters; If the beginning of a group of characters, only the hyphen itself is 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" through "Z".
\b Matches a word boundary, which is the position between the word and the 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 character. The value of x must be one-a-Z or a-Z. Otherwise, c is treated as 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 feed character. Equivalent to \x0c and \CL.
\ n Matches a line feed character. Equivalent to \x0a and \CJ.
\ r Matches a carriage return character. Equivalent to \x0d and \cm.
\s Matches any white space character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].
\s Matches any non-white-space 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 a determined two digits long. 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 match that was obtained. For example, "(.) \1 "matches two consecutive identical characters.
\ n Identifies a octal escape value or a backward reference. n is a backward reference if you have at least n obtained subexpression before \ nthe. Otherwise, if n is an octal number (0-7), then N is an octal escape value.
\nm Identifies a octal escape value or a backward reference. NM is a backward reference if at least NM has obtained the subexpression before \nm. If there are at least N fetches before \nm, then n is a backward reference followed by a literal m. If all the preceding conditions are not satisfied, if both N and M are octal digits (0-7), then \nm will match 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 in four hexadecimal digits. For example, \u00a9 matches the copyright symbol (&copy;).
\< \> The start (\<) and End (\>) of the match word (word). For example, the regular expression \<the\> can match the "the" in the string "for the Wise", but it cannot match the "the" in the string "otherwise". Note: This meta character is not supported by all software.
\( \) The expression between \ (and \) is defined as group, and characters that match the expression are saved to a staging area (up to 9 in a regular expression), which can be referenced using \1 to \9 symbols.
| Two matching criteria are logically "or" (or) operations. For example, regular expressions (him|her) match "it belongs to him" and "it belongs to her", but they cannot match "it belongs to them." Note: This meta character is not supported by all software.
+ Match 1 or more of the character just before it. For example, regular expression 9+ matches 9, 99, 999, and so on. Note: This meta character is not supported by all software.
? Match 0 or 1 of the character just before it. Note: This meta character is not supported by all software.
{i} {i,j} Matches a specified number of characters that are defined by an expression that precedes it. For example, the regular expression a[0-9]{3} can match a string of exactly 3 numeric characters followed by the character "A", such as A123, A348, but does not match A1234. The regular expression [0-9]{4,6} matches any 4, 5, or 6 consecutive digits.

Second, JavaScript's regular expressions are important for form validation and string processing, and common methods associated with regular expressions are:

1, the method of the regular object:

1,1,test (): Used to indicate whether a string matches a regular expression and returns False if the match returns true

1,2,exec (): the EXEC () method retrieves the value of a specified regular expression in a string, and the result is an object that contains all the matching values. If no match is found, NULL is returned

var reg=/(1a|2b). com/;  Used to match all strings containing 1a.com  or 2b.com
	var str= "2221a.com456";
	function Execreg () {
	var result=reg.exec (str);
	Alert (result[0]);   1a.com
	}
You can see the results returned in debug mode:

You can see that the returned result contains an array that holds the matching result: "1a.com" is the result of the entire match, "1a" is the result of the first bracket (1a|2b), and input represents the contents of the string, and index indicates where to start the match from the string.

Slightly modified, you can see the results are as follows:

The result is similar to the above, where 0 is the result of the match, 1 represents the first bracket match, and 2 is the 2nd bracket match, so the entire result: a string comma by 123.

2, the method associated with the regular expression in the string:

2.1 String.match (Pattern): This method returns the same result as the Exec () method of the regular expression, and is an object that contains the same attributes:

<span style= "Font-family:microsoft Yahei, Helvetica neue, Helvetica, Arial, Sans-serif;" ><span>	</span>var reg=/(1a|2b) (3c|4d). com/;  
<span>	</span>var str= "2221a3c.com456";
<span>	</span>function Matchreg () {
	<span>	</span>var result=str.match (reg);
	<span>	</span>alert (result);
<span>	</span>}</span>
See the results returned in debug mode:

Same as the Exec () method of the above regular object returns the result

2.2string.replace (pattern,string): Converts a part of a string that matches a regular expression to a new string:

<span style= "Font-family:microsoft Yahei, Helvetica neue, Helvetica, Arial, Sans-serif;" ><span>	</span>var reg=/(1a|2b) (3c|4d). com/;  
<span>	</span>var str= "2221a3c.com456";
<span>	</span>function Replacereg () {
	<span>	</span>var result=str.replace ( Reg, "* * *");
	<span>	</span>alert (result);//222***456
<span>	</span>}</span>
2.3string.search (pattern); : Find the initial position that matches the string

<span style= "Font-family:microsoft Yahei, Helvetica neue, Helvetica, Arial, Sans-serif;" ><span>	</span>var reg=/(1a|2b) (3c|4d). com/;  
<span>	</span>var str= "2221a3c.com456";
<span>	</span>function Searchreg () {
	<span>	</span>var result=str.search ( REG);
	<span>	</span>alert (result);//3
<span>	</span>}</span>
2.4string.split ();: Dividing a meta string from a match: The resulting result is an array of strings (be sure to pay attention to the effect of the parentheses "()"):

<span style= "Font-family:microsoft Yahei, Helvetica neue,
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.