A detailed description of JavaScript regular Expressions (ii) a detailed explanation of the JavaScript expression functions

Source: Internet
Author: User

Second, JavaScript expression function in the description of the detailed (exec, test, match, replace, search, split)

1. Use Regular Expressions method to match the lookup string

1.1. Exec method Detailed

return value of the Exec method

The Exec method returned is not actually a matching result string, but an object, simply modify the Execreg function, to do an experiment can confirm this point:

function  Execreg (Reg, str) {var result = reg.exec (str); alert (typeof result);} var reg =/b/;var str = ' bbs.bblueidea.com '; Execreg (REG,STR);

The result shows that the type of result is object. and is an array-like object. You can know its properties by using for in: Index input 0. Where index is the index representing the match in the original string, and input is the string representing the input;

As for 0, it means that there is only one matching result, which can be referenced by subscript, which may change the number of matches. We can know the total number of matching results by the length property of the return value.

Based on the above analysis of the return value, modify the Execreg function as follows:

function  Execreg (Reg, str) {var result = reg.exec (str);d ocument.write (' Index: ' +result.index+ ' <br  /> ' + ' Input: ' +result.input+ ' <br  /> '); for (i=0; i<result.length; i++) {document.write (' result[' +i+ ']: ' + result[i]+ ' <br  /> ')}}

Come and experiment Now:

var reg =/\w/;var str = ' bbs.bblueidea.com '; Execreg (REG,STR);

The results are as follows:

Index:0input:bbs.bblueidea.comresult[0]:b

The input string is bbs.bblueidea.com;

The index of the matching B in the original string is 0.

The regular match result is one, B;

var reg =  /(\w) (\w) (. +)/;var  str= ' bbs.bblueidea.com '; Execreg (REG,STR);

The result is:

Index:0input:bbs.bblueidea.comresult[0]:bbs.bblueidea.comresult[1]:bresult[2]:bresult[3]:s.bblueidea.com

As shown in the above two examples, the return object [0] is the content of the entire regular expression. Subsequent elements are the matching contents of each sub-regular expression.

The Exec method updates the regular expression

The Exec method may also update the original regular expression while returning the result object, which depends on whether the regular expression has the G modifier set. Let's take a look at two examples:

var reg =/b/;var str = ' bbs.blueidea.com '; Execreg (REG,STR); Execreg (REG,STR);

The results are as follows:

Index:0input:bbs.blueidea.comresult[0]:bindex:0input:bbs.blueidea.comresult[0]:b

That is, the result of two matches is exactly the same, as you can see from the index, matching the B character at the beginning of the string.

Let's look at how the regular expression with G is set to behave:

var reg =/b/g;var str =  ' bbs.blueidea.com '; Execreg (REG,STR); Execreg (REG,STR);

The results are as follows:

Index:0input:bbs.blueidea.comresult[0]:bindex:1input:bbs.blueidea.comresult[0]:b

It can be seen that the second match is the second B of the string string. This is the function of the G modifier, and below is how exec treats G and non-G regular expressions differently.

If the regular expression does not have a G, then the Exec method does not have any effect on the regular expression, if G is set, then exec updates the Lastindex property of the regular expression, indicating the index of the next character of the matched string after the match. The next time you use this regular expression to match a string, it will begin to match from the last Lastindex property, which is why the above two example results are different.

1.2. Introduction to the test () method of the regular expression

The test method simply checks to see if STR is matched and returns a Boolean value to indicate success. Also create a simple test function:

function  Testreg (REG,STR) {alert (Reg.test (str));}

Example 1

var reg =/b/;var str =  ' bbs.blueidea.com '; Testreg (REG,STR);

Success, Output true.

Example 2

var reg =/9/;var str =  ' bbs.blueidea.com '; Testreg (REG,STR);

Failed, returning false.

2. Use string methods to execute regular expressions

2.1. Match () method

Form: Str.match (REG);

Similar to the Exec method of a regular expression, the method also returns an array-like object, as well as the input and index properties. We define one of the following functions to test:

function  Matchreg (reg,str) {var result =  Str.match (reg); if (result) {document.write (' Index: ' +result.index+ ') <br  /> ' + ' input: ' +result.input+ ' <br  /> '); for (i=0;i<result.length;i++) {document.write (' result[' +i+ ': ' +result[i]+ ' <br  /> ')}}else{alert (' null: Match failed! ‘)}}

For example:

var reg =/b/;var str =  ' bbs.blueidea.com '; Matchreg (REG,STR);

The results are as follows:

Index:0input:bbs.blueidea.comresult[0]:b

Visible, as with exec's results.

But if the regular expression sets the G modifier, the behavior of exec and match is different, as shown in the following example:

Index:undefinedinput:undefinedresult[0]:bresult[1]:bresult[2]:b

A regular expression that has the G modifier set does not stop after a successful match is completed, but continues to find all the characters that can be matched. The results returned include three B. However, input and index information are not provided.

2.2. Replace () method

Form: Str. Replace (Reg, ' new str ');

The function of this is to use the "New str" section of the STR string to match the reg portion of the code, noting that the original string is not modified but returned as the return value. Example:

var reg =/b/;var str =  ' bbs.blueidea.com '; var newstr =  str.replace (reg, ' C ');d Ocument.write (NEWSTR);

The result is cbs.blueidea.com, and only the first B is replaced by C.

var reg =/b/g;var str =  ' bbs.blueidea.com '; var newstr =  str.replace (reg, ' C ');d Ocument.write (NEWSTR);

Output ccs.clueidea.com

Because the G modifier is set, all B is replaced.

var reg =/\w+/g;var str =  ' bbs.blueidea.com '; var newstr =  str.replace (reg, ' word ');d ocument.write (NEWSTR);

Output:

Word.word.word.

Use the $ reference Zhong in the Replace function to match the contents of an expression

Just like in the regular we can use the \1 to refer to the first Zhong the expression matches the content, we can also use the replacement word character in the Replace function to refer to the same content.

Let's look at an example:

var reg =  /(\w+). ( \w+). (\w+)/;var str =  ' bbs.blueidea.com '; var newstr =  str.replace (reg, ' $1.$1.$1 ');d ocument.write (NEWSTR);

The result of the output is:

Bbs.bbs.bbs

First, we know that the first Zhong expression matches to the BBS, then $ $ also represents the BBS. We then set the replacement string to ' $1.$1.$1′, which is actually ' Bbs.bbs.bbs '. Similarly, the blueidea,$3 is the COM.

Looking at an example, reverse the order of two words before and after a space.

var reg =  /(\w+) \s (\w+)/;var str = ' Cainiao  Gaoshou '; var newstr =  str.replace (Reg, ' $ $ ');d Ocument.write ( NEWSTR);

The result is: Gaoshou Cainiao, that is, the word before and after the space is reversed order.

Because $ has a special meaning in the replacement text, we need to write $$ if we want to use the $ character, for example:

var reg =  /(\w+) \s (\w+)/;var str = ' Cainiao  Gaoshou '; var newstr =  str.replace (Reg, ' $$ $$ ');d ocument.write ( NEWSTR);

The result is: $ $.

2.3. Search () method and 2.4. Split () method

Similarly, the search method of the string and the split method can also use regular expressions in the following form:

Str.search (REG);

Search returns the position of the first match of the regular expression. Example:

var reg =/idea/;var str = ' Blueidea '; var pos =  str.search (reg);d ocument.write (POS);

The result is 4.

The following example finds the first non-word character:

var reg =/\w/;var str =  ' bbs.blueidea.com '; var pos =  str.search (reg);d ocument.write (POS);

The result is 3, which is the point "." The location.

Str.split (Reg, ' seprator ');

Split returns the segmented array, for example:

var reg =/\w/;var str =  ' bbs.blueidea.com '; var arr =  str.split (reg);d ocument.write (arr);

The result is: bbs,blueidea,com, the visible string is broken into an array of three elements by a non-word character.

var  reg =/\w/;var  str = ' http://www.baidu.com/'; var  arr = str.split (reg);d ocument.write (arr.length+ ' <br  /> ');d ocument.write (arr);

The result is:

7http,,, Www,baidu,com,

The visible string is divided into an array of 7 elements, including three elements that are empty strings.

Attached: JavaScript regular Expressions in detail (a) Getting Started with regular expressions

A detailed description of JavaScript regular Expressions (ii) a detailed explanation of the JavaScript expression functions

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.