[Aaronyang] The other day, a child told me that JS regular

Source: Internet
Author: User

Follow your own ideas to learn about node. js. Break through the regular unpopular knowledge points, skillfully review the regular common knowledge points

Tags:aaronyang node. js regular Javascript

This blog address: http://www.cnblogs.com/AaronYang/p/3979710.html

Development Preparation (Aaronyang original) (after reading add 20 points)

1 you need to have a browser with developer tools, I'm using a Chrome browser

Come here to study, the first is to consolidate their knowledge, and secondly really need it. Regular getting started simple, high-level understanding is also simple, but because not often used, so over a period of time also forget with the regular similar action: Open the computer, search for files, such as *.doc is to find all the. doc end of the file, * number is a wildcard character. Sql:select * from Persons where the city isn't like '%lon% ' here the percent is a wildcard, find the city that contains the Lon, the following is mostly said to be regular.

2 Basic Review, relatively concise

OK, open Google Chrome, press F12, click ConsoleThe first regular-----HelloWorld, learn to \b
var hello= "Hello World";     var reg=/hello world/;    Alert (reg.test (hello))   //Output True
Here/Regular/(detail), using the regular point test method, test indicates whether the string within the parentheses matches the regular match, matches the return true, does not match the return false here is the Reg match meaning, whether to include Hello World, if you want to find the Hello World, you need to \b. \b denotes the beginning or end of a word, and the demarcation of a word. This means Hello world as a word, the middle cannot contain any characters such as Hello World1 are not compatible, but Hello World Hello World matches if want to complete match, will ^hello world& ^$ table Shows the whole of a regular expression
var reg=/\bhello world\b/;

the second regular-----review the regular basic format

Learn the Slash class character (\w \d. Meaning plus special characters (* +?) {Explicit number}) Additional instructions outside the plus regular (g i) plus regular extra-cut whole and partial characters (^$ and () brackets |)

1. \w letters or numbers or underscores or kanji, memory tips website User name registration can be Chinese

\d number, this is too good to remember, because commonly used

. Any character other than the line break

\s any whitespace character, memory skill S is the abbreviation of space, space is the space bar on the keyboard, blank

The opposite of \d matches the \d, \w,\s \b and so on. For example, \d represents a matching number, and \d is any character other than a number.

\d, [0-9]

\d, [^0-9]

\w, [a-za-z0-9_]

\w->[^a-za-z0-9]

2. Some characters such as.   Number, he does not carry a slash, indicating a regular matching symbol, if you want to match the. NET character, you use \. To replace the dot number. A slash is an escape character, and if you match a character that is already predefined, you'll have to escape it with a slash.

3. Additional description of the character

For example \d+ here the + number means more than 1, \d represents a number, the whole sentence meaning is more than 1 digits, plus equals {1,} so the regular can also write \d{1,}

? Indicates that 0 or 1 * numbers represent 0 or more

The next step is the curly brace type description {1} represents a

{1,} represents no less than 1

{1,10} represents no less than 1, no more than 10

4. /g indicates that the expression will be used to find all possible matches in the input string, and the returned result can be multiple. If you do not add/g, you will only match one

/I is case insensitive when matching

/m for multi-line matching, what is multi-line matching? is a potential match that matches both ends of the newline character. Affects the ^$ symbol in the regular, and some strings contain \ n is more than one line

5. Other

[Enter any character] such as 11[029] to indicate a character that matches 110,112,119

[A-z0-9a-z_] a bit like \w (not Chinese)

[^123] means matching any character other than 3 characters

. * Indicates matching any number of characters that do not contain a newline

6. Using sub-matching

You can use () to match words

var osVersion = "Ubuntu 8"; // 8 of which represents the system major version  number re=/^[a-z]+\s+ (\d+) $/i; // use () to create a sub  -match arr =re.exec (osVersion);  Alert (arr[0]); // The entire osversion, which is the complete match  of the regular expression Alert (arr[1]); // 8, the first sub-match, the fact can also take out the  major version number alert (arr.length); // 2  

If you enclose the character, it is also a group.

Intermediate Regular learning (plus 30 points)

1 0 Wide assertion (matching but not capturing, not capturing or returning a value)

forward 0 Wide assertion (=)Write some examples, I hope you can understand ① (? =express) matches the position of the front of the express match 21 or 24 front position, the front is ay, so match ay, conforms to only Way21 ay24, because the match is AY, does not include 21 and 24 (emphasis), not ay2 1 or ay24, so replace is only ay, not ay21 or ay24 (? =express) belongs to Antecedent Assertion(0-width positive lookahead assertion) begins with the right-hand side of the string to find an ay that ends at 21 or 24, matches to the beginning of the substitution, that is, replacing the ay24 first with Test24, and then Way21 to wtest21. So what's the difference with/ay (21|24)/ig? /ay (21|24)/ig matches only 2 of the two possible, matching find Ay21 or ay24, and then replace the use of split, just start with Ay 21 or 24 end as grouping conditions ===================================      =====aaronyang.cnblogs.com========================================② (see, JS is not supported in the regular) (? <=express) Matches the position behind the Express (JavaScript does not support back lookup) (? <=express) belongs to post-Fat assertion(0 width is being recalled after the assertion), first from the left of the string to find, for example (? <=abc). * Can match DEFGABC in ABCDEFGABC instead of ABCDEFG there is a classic example of how these two usages (? <=\s) \d+ (? =\s)matches numbers separated by whitespace (again, these whitespace characters are not included) negative 0 wide assertion (! If you see an exclamation point, you know it is "non", the opposite means (?! Express) matches the location of the following not express position for example: (?! Express) belongs to Antecedent Assertion, from the right to the left to find Ay, the first is ay24 found ay behind can not with 24, so do not meet, continue to find, find ay23, meet the conditions, split out the first value of Ay24 Aaronyang21 2121 continue to find, find aya22 meet the conditions, split out A22, Keep looking, find ay221,split out 221, the last one Way21 not meet, a separate, the final split result is "Way21", "221", "A22", "Ay24 aaronyang21 2121"Story: BB This string can be understood as a long wood, for the first time in the ay23 place, the AY two letters chisel out, cut a section, and so on, Chisel 4 paragraphs, and then package back========================================aaronyang.cnblogs.com======================================== Of course, the replace is also, unexpectedly see will split, then I changed the story slightly:Story: BB This string can be understood as a long wood, the first time in the ay23, the AY two letters chisel out, leave a hole, and so on, chisel 4 holes, and then fill in the hole Test 4 letters, and finally packaged back)

Of course, there are also those that are not supported in JavaScript.0 Width Negative review post assertion (? <!express) matches the position of the front not express (?: Express) For example: A (?: Y|aronyang) is a more abbreviated expression than ' A|aronyang '

2 greedy and lazy, greedy long, lazy much

When you have a qualifier with a repeating category in a regular expression, add one later? Number, which means open LazyMode

*? Repeat any number of times, but repeat as little as possible
+ repeat 1 or more times, but repeat as little as possible
?? Repeat 0 or 1 times, but repeat as little as possible
{n,m}? Repeat N to M times, but as few repetitions as possible
{n,}? Repeat more than n times, but repeat as little as possible.

Example:

Like ay2013ay2014ay2012ay2014.

The first one is the greedy mode, starting from left to right, beginning with 20, looking for 4 endings, taking the longest string

The second one is lazy mode, starting from left to right, beginning with 20, looking for the nearest 4, forming a value, then continuing to find, composing other values, and finally returning the array

Look at the Replace method, which is the match value first, then the replacement

3 Note (know it well)

. NET regular can have annotations.

Regex regex = new Regex (@ "\ba\w{6}\b", regexoptions.ignorecase); Mostly regexoptions.ignorepatternwhitespace in RegexOptions, ignoring non-escaped whitespace in expressions and enabling annotations marked by #

For example:

Advanced Regular Learning (plus 30 points)

1 recursive group, balanced matching, this is a bit of a mouthful, mainly grouped bar

Balance GroupA class of cases, such as html<div><p>testay</p></div>p can be a group, Div is a group, see whether each label has a closed tag, here to check, The following example can match a nested label
< Div [^>]*>[^<>]* ((? ') Open '<Div[^>]*>] [^<>]*) + ((? ') -open '</div>) [^<>]*) +) * (? ( Open) (?!)) </ Div >

Example 2, print out the value of the href in hyperlink a in HTML
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Text.RegularExpressions;namespaceregmy{classProgram {Static voidMain (string[] args) {Regex reg=NewRegex ("(? <=<a\\s*.*href=\ ") [^\"]* (? =\ ")", regexoptions.ignorecase); stringText ="<a href=\ "http://www.baidu.com/p/yangyanghaoran?from=zhidao\" class=\ "user-name\" target=\ "_blank\" id=\ " User-name\ ">yangyanghaoran<i class=\" i-arrow-down\ "></i></a><a href=\" 333.html\ ">< /a>"; Match m=Reg.            Match (text);  while(m.success) {//Displays the index value at the beginning of the match and the value to matchConsole.WriteLine ("match=["+ M +"]"); CaptureCollection cc=M.captures; foreach(Capture Cinchcc) {Console.WriteLine ("\tcapture=["+ C +"]"); }                 for(inti =0; i < M.groups.count; i++) {Group Group=M.groups[i]; System.Console.WriteLine ("\t\tgroups[{0}]=[{1}]", I, group);  for(intj =0; J < Group. Captures.count; J + +) {Capture Capture=Group.                        CAPTURES[J]; Console.WriteLine ("\t\t\tcaptures[{0}]=[{1}]", J, capture); }                }                //make the next match.m =M.nextmatch ();        } console.readline (); }    }}

This piece with JS is a bit biased, here is interested in their own Baidu study it

=====================================================================================

Note:


Methods for regular Expression objects

Test, which returns a Boolean value that indicates whether a pattern exists in the string being searched. Returns true if it exists, or false if it is present.
exec, which runs a lookup in a string with a regular expression pattern, and returns an array containing the results of the lookup.
Compile, the regular expression is compiled into an internal format, which executes faster.

Match, finds the matching of one or more regular expressions.
Replace, replacing the substring that matches the regular expression.
Search to retrieve the value that matches the regular expression.
Split, splits the string into an array of strings.

The properties of the regular Expression object

Source, which returns a copy of the text of the regular expression pattern. Read-only.
LastIndex, which returns the position of the character, which is the beginning of the next successful match for the found string.
$1...$9, returns nine of the most recently saved parts found during pattern matching. Read-only.
Input ($_), which returns the string that executes the canonical expression lookup. Read-only.
Lastmatch ($&) that returns the last matching character in any regular expression search process. Read-only.
Lastparen ($+), if any, returns the last sub-match during any regular expression lookup. Read-only.
Leftcontext ($ ') that returns the character from the beginning of the string in the searched string to the position before the last match. Read-only.
Rightcontext ($ '), returns the character from the last matching position in the searched string to the end of the string. Read-only.

[Aaronyang] The other day, a child told me that JS regular

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.