-Regular expressions are a matching tool for text patterns.
-Article Guide:
--1. Properties and methods of regular objects
--2. Methods for String objects
--3. Using regular Expressions:
---3.1 to give the string a thousand-minute character
---3.2 Most frequently occurring letters in a string
---3.3 Using regular expressions in multiple rows
---3.4 extracting sub-domains from URLs
---3.5 split Unicode characters
---3.6 using a regular in the Location object
--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------
[New Regular expression]:
1 var // g can be saved 2 var New // A second parameter can be saved
The difference is:
1. Using the literal notation the new regular expression object is generated when the code is compiled and is commonly used in common development.
2. Regular objects generated using constructors are generated when the code is run.
[Using regular Expressions]:
The method of a regular object is used in this way: RegExp object. Method (String)
The method of a string object is to use the following: String. Method (RegExp object)
The properties and methods of a regular object
1.1 The properties of the regular object:
1 // returns a Boolean value that indicates whether the regular object is set with the I modifier, which is a read-only property 2 // indicates whether the regular object has the G modifier set and returns a Boolean value 3 // indicates whether the M modifier is set and returns a Boolean value 4 // returns the location of the next start of the search, which is read-write, but only meaningful when the G modifier is set 5 // returns the string form of a regular expression (excluding backslashes), read-only properties
This is done using:
1 I -- perform a ignore case match 2 G -- perform a global match (you can see all matches instead of just finding the first match and then stopping)3 m --Perform multi-line matching
1.2 Test method
The test method returns a Boolean value that verifies whether a string conforms to a pattern. If the regular expression has a G modifier, each test method starts at the point where the last match ended.
A regular object that uses the G modifier, which indicates where the search is to be recorded, and then uses the test method, each time the search is started in the last position of the match:
If the regular expression is an empty string, all strings will be matched, but only if you use the new RegExp () method:
1.3 Exec Method
EXEC () returns the matching result, the matching successful exec method returns an array with matching results, and the matching failure returns null:
If the regular expression contains parentheses, the returned array will include more than one element. The first is the result of a successful match, followed by the result of a successful match in parentheses, and if there are multiple parentheses, the result of their successful match will be the array element:
The array returned after calling the Exec method has the following two properties:
1 Input-- the entire original string to be matched 2 Index--The starting position of the entire pattern matching success
Similarly, a regular expression with the G modifier can be used multiple times with the Exec method, and the next search position starts at the end of the last successful match.
If the regular object is an empty string:
Ii. Methods of String objects
1 Match----- returns the matched substring 2 Search----Searches 3 Replace--- replacement 4 Split-----Split
2.1 Match () method
The match method of a string object is similar to the Exec method of a regular object:
But if the regular expression has a G modifier, then the match method differs from the Exec method:
You can see that match returns all the results of a successful match, but the Exec method returns only one.
2.2 Search method
The search method returns the index of the successful position in the entire string, and returns the -1,search method ignores the G modifier itself if no match succeeds any characters.
2.3 Replace method
Str.replace (' Search mode ', ' replace content ');
As you can see, if the regular expression does not have a G modifier, the Replace method replaces the result of the first successful match and, if the G modifier is added, replaces all successful values of the match.
The second parameter of the Replace method can use the $ symbol to refer to what you want to replace:
1 $&----- refer to matching string 2 $ '----- match result preceding text 3 $ '-----match result text after 4 $n-----refers to the nth set of content that matches successfully, and N starts at 1 5 $$-----means dollar sign $
The second parameter of the Replace method can also be a function that replaces the value that the first parameter matches with the return value of the function.
Also, a function that is the second parameter of the Replace method can be used by more than one parameter. Its first parameter is the entire matching content, the second parameter is the group match (at this time how many groups match the number of parameters), in addition to the last two parameters can be added, the penultimate parameter is the capture of the content in the entire string position, the last parameter is the original string.
2.4 Split () method
Split (' Split regular ' of string ', ' returns the maximum number of members of an array '); Returns an array of parts after the split.
You can transform regular matching rules to split a string.
The regular matching rules above are divided by 0 or more, and if parentheses match the parentheses, the segmentation rules are returned as members of the array.
Iii. using regular expressions
Regular expression is not only flexible but also very powerful, with it can be very concise implementation of a lot of practical functions, the following simple list of some:
3.1 Adding a character to a string
1 function spli (str) {2 var re =/(? = (?! \b) (\d{3}) +$)/g; 3 return str.replace (Re, ', '); 4 }5 console.log (spli (str));
Which (? =exp) is used to match the position of the exp front, (?! EXP) matches the position followed by the exp, they are all 0 wide assertions.
If you don't use regular expressions, the code will be more:
1 varstr = ' 12345432 ';//->[]2 functionspli (str) {3 varINum = str.length%3;//->0,1,24 varprev = ' ';5 vararr = [];6 varInow = 0;7 varTMP = ';8 //Chiphi is from the back forward three bit three bit plus, so the three bits out of the front extracted from the9 if(INum!=0){TenPrev = str.substring (0, iNum); One Arr.push (prev); A } - //take the extra part back. -str =str.substring (iNum); the for(vari=0;i<str.length;i++){ -inow++; -TMP + =Str[i]; - if(Inow==3 &&tmp) { + //The array contains three-bit numbers. - Arr.push (TMP); +TMP = '; AInow = 0; at } - } - returnArr.join (', '); - } -Console.log (Spli (str));
View Code
3.2 The most frequently occurring characters in a string
1 varstr = ' KKKKKKKAAAKKKKKKAAAAASDDD ';2 functionMost (str) {3 //sort, regular match subkey4 vararr = str.split (' ');5 Arr.sort ();6str = arr.join (' ');7 //\1 matches the same as before, that is, the same as (\w)8 varRe =/(\w) \1+/G;9 varnum = 0;Ten varValue = '; One //the one here is to find the repetition of the whole AStr.replace (Re,function($0,$1){ - //alert ($); - if(num<$0. Length) { thenum = $. Length; -Value = $; - } - }); + return' The most frequently occurring character is ' +value+ ' appears ' +num+ '; - } +Console.log (most (str));
Where \1 represents the text for grouping 1 matches.
3.3 Using regular expressions in multiple rows
The ^ symbol is used to match the start of the input/string. If the multiline (multiline) flag is set to True, the character also matches the beginning of a line break character.
3.4 Extracting sub-domains from URLs
The above regular match/start string matches to the first one. It stops, http://is 7, so the subdomain is returned after using substr (7).
3.5 Dividing Unicode characters
The \w and \w that are commonly used to match characters will only match the basic acscii characters, and the Unicode characters will have to be dealt with separately:
3.6 using a regular in a Location object
Here's an example of the more I look dizzy, the amount:
From the moustache = =. [Play positive is highlight highlight]
Iv. Other
Collection of regular Expressions:
[Common regular Expression collection]
[JS Common Regular Expressions]
Resources:
[RegExp MDN]
[Play positive is highlight highlight]
Here I'm just summarizing the methods and properties related to regular expressions, it is highly recommended to read: Regular expressions 30-minute introductory tutorial it teaches you how to write regular expressions, and then combined with the above mentioned methods, writing, it is not so far away, very practical oh.
Real-combat JS Regular Expression