The regular expression __html5 of JS

Source: Internet
Author: User

Full name: Regular expressions, abbreviation regexp;
Function: The RegExp object is used to specify what is retrieved in the text.
Two methods are created:
1. Normal way: var reg=/expression/Additional parameter
Expression: A string that represents a rule in which special characters can be used to represent special rules, which are described later in detail.
Additional parameters: to extend the meaning of an expression, there are currently three main parameters:
G: Delegates can make global matches.
I: represents case-insensitive matching.
M: Represents a multiple-row match.
The above three parameters, can be any combination, representing the compound meaning, of course, can not add parameters.
Example:
var reg=/a*b/;
var reg=/abc+f/g;
2. Constructor mode: Var reg=new RegExp ("expression", "additional parameters");
Example:
var reg=new RegExp ("a*b");
var reg=new RegExp ("Abc+f", "G");

The difference between normal mode and constructive function
1. The expression in the normal mode must be a constant string, and the expression in the constructor can be a constant string or a JS variable, such as an expression parameter based on the user's input, and so on:
var reg=new RegExp (Document.forms[0].exprfiled.value, "G");
2. It is important to note that constructor parameters are strings wrapped in double quotes without the need for a forward slash package. If the regular object mode, Regexg received a string, \ Backslash is the escape character, \d will become D, at this time need to use two backslashes, that is, \d to achieve \d effect, but in the literal way, is not a string, so use a backslash;
Tools:
Http://tools.jb51.net/regex/javascript

Second, difficult
1. (?:p attrn) matches pattrn without obtaining a matching result, which means that this is a non fetch match and is not stored for later use. This is useful for combining parts of a pattern with the "or" character (|). For example, ' Inustr (?: Y|is) is a more abbreviated expression than ' Inustry|inustris '.
As an example:

var rchecked =/checked\s* (?: [^=]|=\s*.checked.) /I;  
Print true,\s* is greedy match, that is, jump to the end of the space, that is, skip all the spaces, and then go to match the characters behind! If the back doesn't match, then backtrack!  
Alert (rchecked.test ("checked"));  
Print true, because it is greedy match, then \s* will skip all the blanks to match the following characters, if there is no match will backtrack, so there can be any number of blanks before and after the equals sign!  
Alert (Rchecked.test ("Checked=xcheckedx"));  

Print checked a 
alert ("Checked a=xchecked". Match (rchecked));  
Print checked A,a
var rchecked =/checked\s* ([^=]|=\s*.checked.) /I; 
Alert ("Checked=xchecked". Match (rchecked));  

is not to save the back, a, this should be the difference bar;
2. (? =pattrn) forward lookup, matching the search string at the beginning of any matching pattrn string. This is a non-fetch match, that is, the match does not need to be acquired for later use. For example, ' Winws (? =95|98| nt|2000) ' Can match "winws" in "Winws 2000", but cannot match "winws" in "WINWS 3.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.

3. (?!) PATTRN) to match the lookup string at the beginning of any mismatched ngativ lkaha maths th Sarh string at any pint whr a string nt mathing pattrn. This is a non-fetch match, that is, the match does not need to be acquired for later use. For example ' WINWS (?! 95|98| nt|2000) ' Can match "winws" in "WINWS 3.1", but cannot match "winws" in "WINWS 2000". 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.

4. After the regular expression is constructed, it can be evaluated like a mathematical expression, that is, it can be evaluated from left to right and in order of precedence. Priority is as follows:
1. \ escape Character 2. (), (?:), (? =), parentheses and square brackets 3. *, +,?, {n}, {n,}, {n,m} qualifier 4. ^, $, \anymtaharatr position and order 5.| " or the action
Note that | is the last priority, so it's not going to run next to |

5. If in the [] character set, a lot of characters do not need to be escaped, this should be noted, Zhu Junyi can also;
Error:
1. To correctly understand the escape of strings with quotes and the escaping of regular expressions, you can't confuse the two. The function of his two is completely different: the characters in the regular expression are meant to match their corresponding content, so there are some special meanings, and the escape of the string simply expresses some special characters in quotation marks;
And one place that needs a lot of attention is that the regular expression is followed by a string, so it needs to be regexp, on the other hand, the need to double escape what is really needed;
2. The escape character "\" is usually escaped after the normal character is itself, In addition to the regular expression has already specified some special meaning of the characters;
3. Confusing one thing: when double quotes are in a normal string, the backslash "\" We do calculate is an escape character, so that "\" is required in the regular expression to be "\". This is actually the difference between a regular expression string and a generic string, the normal string escape is done, do not need to escape the "\" To do the operation, and the regular expression is after parsing the string again after the match, equal to two times, which is their fundamental difference; string "" is different from the "" in the New RegExp (""); the backslash in the
4.js string actually represents the escape, which you want to use, or it should be escaped directly to the Unicode value if it appears inside the string; "\u2213" actually represents a character. , so the last thing you want to see is the character "\u2213"-> the corresponding character, or the encoded value "\u2213"->\u2213
Regular expression is too much, can only be used every time to check, here is a man wrote a very full, In addition to the backslash is written as a slash, the other is very full.
URL: http://blog.csdn.net/zaifendou/article/details/5746988
http://www.jb51.net/article/72346.htm
Two days ago to see a good thing, but also regular expression, the Web site first to remember it
URL: http://www.jianshu.com/p/e7bb97218946
http://deerchao.net/tutorials/ Regex/regex-1.htm

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.