Take JavaScript notes (6)-Regular Expression basics 2

Source: Internet
Author: User
Continue with the regular expression & mdash; quantifiers: [javascript]/*** you can specify the number of times a specific pattern appears. When you specify the number of times that a mode should appear *, you can specify the hard number (for example, a character should appear three times), or * You can specify the soft number (for example, this... syntaxHighlighter. all (

Continue regular expression-quantifiers:
[Javascript]
/**
* Quantifiers can specify the number of times a specific mode appears. When a specified mode appears
* When the number of times, you can specify the hard number (for example, a character should appear three times), or
* Soft quantity can be specified (for example, this character should appear at least once, but it is acceptable
* Repeat any time ).
Code Description
? Zero or one occurrence
* Zero or multiple occurrences (any)
+ Appears once or multiple times (at least once)
{N} must appear n times
{N, m} appears at least n times, but not more than m times
{N,} appears at least n times

For example, you want to match the words bread, read, and red.
*/
Var sIf =/B? Rea? D /;
SIf =/B {0, 1} rea {0, 1} d/; // ANOTHER METHOD

/**
*
* For example, match bd, bad, baad, baaad
*
* Regular Expression matching
Ba? D "bd", "bad"
Ba * d "bd", "bad", "baad", "baaad"
Ba + d "bad", "baad", "baaad"
Ba {0, 1} d "bd", "bad"
Ba {0,} d "bd", "bad", "baad", "baaad"
Ba {1,} d "bad", "baad", "baaad"
*/

/**
* Matching: bead, baed, beed, baad, or bad
*/
// Var eaTest =/B [ea] {1, 2} d /;
Var sToMatch = "abbbaabbbaaabbb1234 ";
Var re1 =/. * bbb/g; // greedy
Var re2 = /.*? Bbb/g; // inert www.2cto.com
Var re3 =/. * + bbb/g; // Control
Alert ("re1:" +re1.exe c (sToMatch) + "\ n -- re2:" +re2.exe c (sToMatch ));
Greedy, inert, and supporting quantifiers
Greedy quantifiers first check whether the entire string matches. If no match is found, it is removed.
The last character in the string and try again. If no matching is found
, Remove the last character again. This process will be repeated until
Match or the string contains no characters.
The inertia quantizer first checks whether the first character in the string matches.
If the character is not enough, the system reads the next character and a string consisting of two characters. If you still
No matching is found, and the inertia quantizer continues adding characters from the string to know that the matching is found.
Or the entire string has been checked and does not match. The work of inert quantifiers and greedy quantifiers
The method is the opposite.
The dominant quantizer only attempts to match the entire string. If the entire string cannot produce a match,
Do not try again. The dominant word is simply a one-size-fits-all approach.
How to express greed, inertia, and dominant quantifiers? The asterisk, plus sign, and question mark are used completely.
For example, a single question mark (?) It's greedy, but the question mark is followed by a question mark (??) Just
It is inert. To make the question mark a dominant quantizer, add a plus sign (? + ).

Greedy inertia control description
? ?? ? + Zero or one appearance
**? * + Zero or multiple occurrences
++? ++ Appears once or multiple times
{N }? {N} + appears exactly n times
{N, m} {n, m }? {N, m} + appear at least n times at most m times
{N ,}{ n ,}? {N ,}+ appear at least n times
--------------- Original article from javaScript advanced programming -----------------------
Complex Mode-group:
[Javascript]
/**
* Group,
* Assume that you want to match the string "dogdog ". Use current knowledge
* Possible estimation expressions should be similar:
*/
Var res =/dogdog/g;
Alert (res. test ("dogdog "));
 
// Rewrite the group:
Var res =/(dog) {2}/g;
Alert (res. test ("dogdog "));

// Grouping quantifiers
Var re1 =/(dog )? // Zero or one occurrence
Var re2 =/(dog) */; // zero or multiple occurrences (any)
Var re3 =/(dog) +/; // appears once or multiple times (at least once)

// You can also place groups in the middle
Var res =/(mmm (and nnn )?) /;
Trim () method:
[Javascript]
/**
* Trim () method
* "\ S" matches all spaces.
* "." The decimal point can match any character except the line break (\ n.
* "^" Matches the start of the string, but does not match any character.
* "{1} quot; matches the end of the string and does not match any character.
* Var reExt =/^ \ s *(.*?) \ S + $ /;
* This regular expression searches for zero or multiple spaces starting with a string,
* Followed by any number of characters (characters captured in the group ),
* The end of the last string is zero or multiple spaces.
* Use the String object's replace () method and reverse reference,
* You can customize your trim () method:
*/
String. prototype. trim = function (){
Var reExt =/^ \ s *(.*?) \ S + $ /;
/*
* $1 indicates the characters in brackets in the left expression, that is, the first child match,
* Similarly, $2 indicates the second child match. What is a child match? In layman's terms,
* Each bracket on the left is the first word match, and the second bracket is the second child match.
*/
Return this. replace (reExt, "$1 ");
};
Alert ("'" + "fdsa". trim () + "'");
Reverse reference:
[Javascript]
/**
* Reverse reference
* Each group is stored in a special place for future use.
* The Special values stored in the group are called reverse references.
* First, after the test (), match (), or search () method of the regular expression object is used,
* The Reverse reference value can be obtained from the RegExp constructor.
*/
Var reg =/# (\ d + )/;
Alert (reg. test ("#123456789 "));
/*
* The output content is (\ d +) matched: 123456789
* If there is a second group (), then $2 is pushed accordingly.
*/
Alert ("'" + RegExp. $1 + "'");
 
/**
* Example of reverse reference:
*/
Var reg =/(\ d {3}) (\ d {3 })/;
/*
* $1 is equivalent to: 456
* $2 is equivalent to: 123
*/
Alert ("456123". replace (reg, "$2 $1 "));
Candidates:
[Javascript]
/**
* Candidates
* An expression that matches "The Ancient West Wind" and "Dan"
* "|" Pipeline character: Between Expressions on both sides of the left and right "or" relationship"
*/
Var reg =/(west wind in the ancient road | Dan )/;
Alert (reg. test ("Dan s "));
Non-capturing group:
[Javascript]
/**
* Non-capturing Group
* Storing reverse references in long regular expressions reduces the matching speed.
* By using non-capturing groups, you can still have the same capabilities as matching string columns,
* There is no overhead for storing results.
* To create a non-capturing group, add a question mark and a colon following the left parenthesis:
*/
Var reg = /#(? : \ D + )/;
Reg. test ("#456123 ");
Alert (RegExp. $1 );
Remove all HTML tags from the text:
[Javascript]
String. prototype. stripHTML = function (){
Var reTag =/<(? :. | \ S) *?> /G;
Return this. replace (reTag ,"");
};
/*
* If you want to use double quotation marks as strings, add \ escape
*/
Alert ("1". StripHTML ());
Foresight:
[Javascript]
/**
* Foresight
* It tells the regular expression generator to look forward to some characters without moving their positions.
* There are also positive and negative prospects.
* Forward lookup checks whether a specific character set is displayed next.
* Negative forward view checks the specific character set that should not appear next.
* To create a forward view, place the mode in (? Between = and.
*/
Var testStr = "begRooms ";
Var testAry = "begTest ";
Var reg =/beg (? = Room) // match the beg following the Room
Alert (reg. test (testStr); // true
Alert (reg. test (testAry); // false

/*
* Negative foresight
* To create a schema, place the schema in (?! And.
*/
Var reg =/beg (?! Room) // match the beg that does not follow the Room
Alert (reg. test (testStr); // false
Alert (reg. test (testAry); // true
Boundary:
[Javascript]
/**
* Boundary
* \ W: match any word character that contains underscores, equivalent to [A-Z a-z 0-9 _]
* \ W: match any non-word character, equivalent to [^ A-Z a-z 0-9 _]
* \ B: Word boundary
* \ B: Non-word boundary
*/
Var sT = "my English is very shit .";
Var reg =/(\ w +) \. $ /;
Alert (reg. test (sT); // true
Alert (RegExp. $1); // shit

Var reg =/^ (\ w + )/;
Alert (reg. test (sT); // true
Alert (RegExp. $1); // my

Var reg =/^ (. + ?) \ B /;
Reg. test (sT );
Alert (RegExp. $1); // my

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.