Learn JavaScript notes (7) -- RegExp object and common Regular Expressions

Source: Internet
Author: User
Tags expression engine
JavaScriptRegExp object: global & mdash; Boolean value, indicating whether ignoreCase & mdash; Boolean value has been set for g (global Options), indicating I (case-insensitive options) whether the lastIndex & mdash; integer is set to indicate the position of the next matching character (only when

JavaScript RegExp object:
Global -- Boolean value, indicating whether g (global option) has been set
IgnoreCase -- Boolean value, indicating whether I (case-insensitive) is set
LastIndex-integer, indicating the position of the next match starting from (only when exec () or test () function is used, otherwise it is 0)
Multiline -- Boolean value, indicating whether m (multi-row mode option) has been set
Source -- the source string of the regular expression. For example, the expression/[ba] */'s source will return "[ba] *".
Generally, the global, ignoreCase, multiline, and source attributes are not used, because the data is known before.
The really useful property is lastIndex, which tells you how far the regular expression looks before it stops in a string.
[Javascript]
Var sToMatch = "bbq is short for barbecue ";
Var reB =/B/g;
ReB.exe c (sToMatch );
Alert (reB. lastIndex); // out: 1
ReB.exe c (sToMatch );
Alert (reB. lastIndex); // out: 2
ReB.exe c (sToMatch );
Alert (reB. lastIndex); // out: 18
ReB.exe c (sToMatch );
Alert (reB. lastIndex); // out: 21
The regular expression reB looks for B. When it detects sToMatch for the first time,
It finds that in the first position -- that is, the position 0 -- is B;
Therefore, the lastIndex attribute is set to 1,
When exec () is called again, execution starts from this place.
Call exec () Again, expression in position 1 again found B,
So we set the value of lastIndex to 2. When the third call, we found that B is at the position 17,
So we set the value of lastIndex to 18.
If you want to match from the beginning, you can set lastIndex to 0.
Static attributes:
[Javascript]
/**
* They all have two names: a complex name and a short name starting with the dollar sign.
* Long Name Short Name Description
* Input $ _ the final string used for matching (the string passed to exec () or test)
* LastMatch $ & last matched characters
* LastParen $ + Last matched group
* LeftContext $ 'the substring before the last match
* Multiline $ * specifies whether all expressions use the Boolean value of multiline mode.
* RightContext $ 'substring after the last match
*/
Var sToMatch = "the west wind of the ancient road is thin horse, Xi Yang Xixia, And heartbroken people are at the end of the world! ";
Var reB =/(West) Wind/g;
ReB. test (sToMatch );
Alert (RegExp. $ _); // out!
Alert (RegExp. leftContext); // out: ancient road
/*
* When using a short name, some characters are invalid. []
* ["$ '"]
* ["$ +"]
* ["$ &"]
* ["$ '"]
*/
Alert (RegExp ["$ '"]); // out: Tianya!
Common Regular Expression verification-original link: http://topic.csdn.net/u/20080820/14/dadb903c-8724-422e-a3bd-f2f1c4e812c6.html
[Javascript]
^ \ D + $ // match a non-negative integer (positive integer + 0)
^ [0-9] * [1-9] [0-9] * $ // match a positive integer
^ (-\ D +) | (0 +) $ // match a non-positive integer (negative integer + 0)
^-[0-9] * [1-9] [0-9] * $ // match a negative integer
^ -? \ D + $ // match the integer
^ \ D + (\. \ d + )? $ // Match non-negative floating point number (Positive floating point number + 0)
^ ([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ // matches the Positive floating point number
^ (-\ D + (\. \ d + )?) | (0 + (\. 0 + )?)) $ // Match a non-Positive floating point number (negative floating point number + 0)
^ (-([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ // matches a negative floating point number.
^ (-? \ D +) (\. \ d + )? $ // Match floating point numbers
^ [A-Za-z] + $ // match A string consisting of 26 English letters
^ [A-Z] + $ // match a string consisting of 26 uppercase letters
^ [A-z] + $ // match a string consisting of 26 lowercase letters
^ [A-Za-z0-9] + $ // match a string consisting of digits and 26 letters
^ \ W + $ // match a string consisting of digits, 26 English letters, or underscores
^ [\ W-] + (\. [\ w-] +) * @ [\ w-] + (\. [\ w-] +) + $ // match the email address
^ [A-zA-z] +: // match (\ w + (-\ w + )*)(\. (\ w + (-\ w + )*))*(\? \ S *)? $ // Match the url
Regular Expression matching Chinese characters: [\ u4e00-\ u9fa5]
Match double-byte characters (including Chinese characters): [^ \ x00-\ xff]
Application: Calculate the length of a string (two-byte length Meter 2, ASCII character meter 1)
String. prototype. len = function () {return this. replace ([^ \ x00-\ xff]/g, "aa"). length ;}
Regular Expression for matching empty rows: \ n [\ s |] * \ r
Regular Expressions matching HTML tags:/<(. *)>. * <\/\ 1> | <(. *) \/>/
Regular Expression matching spaces at the beginning and end: (^ \ s *) | (\ s * $)
[Javascript]
/* Regular Expression example
* 1, ^ \ S + [a-z A-Z] $ cannot be blank cannot have spaces can only be English letters
* 2. \ S {6,} cannot be empty for more than six characters
* 3. ^ \ d + $ cannot contain spaces or non-numbers.
* 4. (. *) (\. jpg | \. bmp) $ can only be in jpg and bmp formats.
* 5. ^ \ d {4} \-\ d {} $ can only be in the format of 2004-10-22
* 6. select at least one item for ^ 0 $.
* 7. ^ 0 {2,} $ select at least two items.
* 8. ^ [\ s | \ S] {20,} $ cannot be empty for more than 20 words
* 9. ^ \ +? [A-z0-9] ([-+.] | [_] + )? [A-z0-9] +) * @ ([a-z0-9] + (\. | \-) + [a-z] {2, 6} $ email
* 10. \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * ([,;] \ s * \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * enter multiple addresses separated by commas (,) or spaces.
* 11, ^ (\ ([0-9] + \))? [0-9] {87341628} $ a telephone number with a 7-or 8-digit phone number or a District Code such as (022)
* 12, ^ [a-z A-Z 0-9 _] + @ [a-z A-Z 0-9 _] + (\. [a-z A-Z 0-9 _] +) + (\, [a-z A-Z 0-9 _] + @ [a-z A-Z 0-9 _] + (\. [a-z A-Z 0-9 _] +) * $
* Only letters, numbers, and underscores are allowed. @ and. must be in the same format as a standard email.
* 13 ^ \ w + @ \ w + (\. \ w +) + (\, \ w + @ \ w + (\. \ w +) * $ the above expression can also be written in this way, which is more refined.
14 ^ \ w + (-\ w +) | (\. \ w +) * \ @ \ w + ((\. |-) \ w + )*\. \ w + $
Supplement:
[Javascript]
21. Regular Expression for matching empty rows: \ n [\ s |] * \ r
21. Regular Expression matching empty rows:/^ \ s * $/m

22. Regular Expressions matching HTML tags:/<(. *)>. * <\/\ 1> | <(. *) \/>/
22. Regular Expressions matching HTML tags:/<([^>] +)> [^ <>] * <\/\ 1> | <([^>] +) \/>/
This actually does not play a major role, because regular expressions cannot support nested matching. Only the. NET Expression Engine has this function.

23. Regular Expression matching spaces at the beginning and end: (^ \ s *) | (\ s * $)
23. Regular Expression matching spaces at the beginning and end: ^ \ s + | \ s + $
Add the trim () function to the String of JavaScript:
String. prototype. trim = function (){
Return this. replace (/^ \ s +/, ''). replace (/\ s + $ /,'');
}

* 4. (. *) (\. jpg | \. bmp) $ can only be in jpg and bmp formats.
* 4. ([^.] +) \. (jpg | bmp) can only be in jpg and bmp formats.

* 8. ^ [\ s | \ S] {20,} $ cannot be empty for more than 20 words
* 8. ^ \ S {20,} $ cannot be empty for more than 20 characters

Common regular links:
Http://www.aslibra.com/doc/regex.htm
Http://www.jb51.net/article/21484.htm
Http://www.blueidea.com/tech/program/2004/2273.asp
Http://www.douban.com/group/topic/3402632/
Http://www.blogjava.net/Vikings/archive/2006/01/06/26893.html

From Dan's column

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.