What is regular expression?

  • Regular expressions are used to work with strings, and are good at dealing with complex strings.
  • Model of the definition of the regular expression is a string (or called pattern, the pattern in English), we can use this model to verify whether a string (or part of a string in) and the model match (or mode), or the use of this model to a string and that part of the model matching to find out.

A re defines a string model. The first function of the re is to verify that a string matches the model. The second purpose of re is to “find out what matches.”

  • In fact, the re only defines a string model, how to verify the string and find the string, is the method on the re class to complete.

Var reg=/\d+/;

Written between two slashes is a syntax specification that defines a regular object. \d denotes a number in a re, and + denotes one or more occurrences. So that defines a model of numbers that appear one or more times. It matches "ab23839cd", "a4d", "33928", "3490CF", "0938FA", and "z9" because each of these strings contains one or more digits that will return true if tested using the test method of the re class.Copy the code

Ex. :

var reg=/\d+/; Var STR = "ab23839cd"; alert(reg.test(str)); True / / pop upCopy the code

The test method, which is the regular class method, takes a string and is responsible for verifying that STR matches the pattern defined by REg. Let’s define another string:

Var str2 = "abcdef"; alert(reg.test(str2)); // This time the popup is false because str2 has no numbers.Copy the code

If you want to find the string that matches the validation, you need to use another method, for example:

alert(reg.exec(str)); 23839 alert(reg.exec(str2)); // Pop null because reg and STR2 do not match.Copy the code

Of course, I can also use the String match method to find a match for the reg, for example:

alert(str.match(reg)); Exec and match, both powerful methods for handling strings, will be explained in more detail in a later section.Copy the code

If one or more digits are strictly matched and no other characters can appear, it should be written like this:

	var reg=/^\d+$/;
Copy the code

This model represents numbers from the beginning to the end. ^ indicates that the following digit must be at the beginning, and $indicates that the preceding digit must be at the end.

  • Symbols like ^, $, \d, and +, which represent special meanings in the re, are called metacharacters.Copy the code

Metacharacters are not the only characters allowed in regex. Regular characters are also allowed. A regular character in a re that represents the character’s original meaning. Such as:

var reg=/^\d8\d$/; // Matches a string containing only three digits with an 8 in the middle. Best answer: ^\d means to start with any number, one \d means to appear once; The middle 8 denotes the character 8 itself, followed by \d$denotes an arbitrary numeric ending. So this re will match "282", "389", "081", etc., but not "1899", "819", "08".Copy the code

In /^\d8\d$/, the two \d’s at the beginning and the end, and the 8 in the middle, represent the same character. They are the basic units of the regular. We call them “atoms”. For more on the basics, see below

1. Define re

1. There are two ways to create a re:
Var reg = / abcd /; Var reg=new RegExp(' abcd ')Copy the code

These two definitions are the same

2, if there is a pattern modifier, such as the full text of the string abcd, the two forms are: (g is the pattern modifier, meaning that the whole string is searched multiple times)
var reg=/abcd/g; Var reg = new RegExp (' abcd ', 'g');Copy the code
3, should pay attention to in the case of a, is if in the regular slashes “\” (enter the above slash), when the object using the constructor to create a regular, want to escape, such as:

Reg = new RegExp(“\w+”)//

Metacharacters common examples

An introduction to the exec method of regular expressions

Grammar:

reg.exec(str); 
Copy the code

Where STR is the target string to execute the regular expression. Such as:

<script type="text/javascript"> var reg = /test/; var str = 'abcdtestString'; var result = reg.exec(str); //result is an array,result[0] holds the string "test" alert(result); </script>Copy the code

Test pops up because the regular expression reg matches the part of ‘test’ in STR (that is, the string ‘abcdtestString’) and stores’ test ‘in the first item of an array. Then assign the array return to Result.

Exec returns an array with length 1: [” test “]. When alert(result) is returned, the system automatically executes [” test “].tostring ().

Add: Change the re above:

<script type="text/javascript"> var reg = /(t)(e)(s)(t)/; Var STR = 'abcdtestString'; var result = reg.exec(str); / / the result now becomes [" test ", "t", "e", "s", "t"] alert (result); // test,,t,e,s,t </script>Copy the code

If parentheses are added to each atom in the regular, it means that there are several sub-regulars in a large regular. Therefore, exec not only finds out the characters matched by the total regular, but also finds out the characters in the sub-regular and puts them in the array one by one. With regard to sub-regulars, we can do a brief understanding of them first, which will not be entangled in this knowledge. Exec is a powerful method, described in more detail below

Fourth, and re – related methods of detailed explanation

Detailed explanation of EXEC method (key and difficult points)

  • The return value of the exec method
  • The exec method returns an array var reg = /b/; var str='bbs.zhufengpeixun.cn'; var result = reg.exec(str);Copy the code

Let’s use the for in loop to iterate over what additional properties the array has:

For (var attr in result){console.log("reslut attribute: "+attr+", +result[attr])} 0, value of this attribute: b [result[0] = index 0, value of this attribute: 0] reslut: index, value of this attribute: 0 Bbs.zhufengpeixun. Cn [this property indicates the original string]Copy the code

There is also a non-enumerable attribute length that is not printed to the console. If there are groups in the re, the value of length is not 1. Such as:

Var reg = /(\w)(\w)(\w)/ // without mode modifier g. var str='bbs.zhufengpeixun.cn'; var result=reg.exec(str); Var result=reg.exec(STR); For (var attr in result){console.log("reslut attribute: "+attr+", this attribute value: "+result[attr])Copy the code

}

The result is:

For reslut: 0, BBS for this property: 1, b for this property: B: reslut property: 3, s: index: 0 Input, the value of this property: bbs.zhufengpeixun. CnCopy the code
Result. length now has a value of 4, which means that four matches are stored in result, and result[0] is what the entire regular expression matches. Result [1],result[2], and result[3] are the matching contents of each sub-regular expression (grouping).

2. Update the regular expression by exec method

Depending on whether the g modifier is set to the regular expression, the exec method may also update the original re (note that the re object is updated) while returning the result object. Let’s start with two examples:

var reg = /(\w)(\w)(\w)/g; Var STR ='bbs.zhufengpeixun.cn'; reg.exec(str); Var result=reg.exec(STR); For (var attr in result){console.log("reslut attribute: "+attr+", +result[attr])}Copy the code

The output is:

Zhu 【 zhu】 reslut: 1, z reslut: 2, h reslut: 3, zhu 【 zhu】 reslut: 1, z reslut: 2, h reslut: 3, zhu 【 U index of reslut: 4 [zhu 'appears in STR index 4] input of reslut: bbs.zhufengpeixun. CnCopy the code
As you can see, the exec method continues to match and find the second time, as well as a third and fourth time. That’s where the G modifier comes in. If it’s multiple searches, how do you know where to start next? This is where a very important property of the regex comes into play, called lastIndex.
  • Each re instance has an attribute called lastIndex, which specifies where the current match started.
  • If the regular expression does not set the pattern modifier G, the value of lastIndex is always 0, which means that no matter how many times the re is used, it is always matched from position 0 in the string.
  • So even though the exec method in section 1 is executed twice, it has no effect on the value returned, because the exec method is executed from scratch each time.
  • If g is set, exec will update the lastIndex attribute of the regular expression to indicate the index of the next character in the string to be matched. The next time the regular expression is used to match the string, the lastIndex attribute will be matched from the last one. That’s why the two examples above turned out differently.

It’s important to note that the lastIndex attribute is an attribute of the re object; The index and input attributes are properties of the array returned by the exec method.

Two points are emphasized:

  • Even if the g modifier is set for the re, the exec method does not automatically perform a full-text lookup, but modifies the lastIndex of the re object.
  • Two, but its specialty lies in not only captures the content of a regular match, can also capture the child regular (branch) matched to the content, if you want to put the string all the matches and the match to (that is, the total regular and child regular matches took to), the need to customize the following such a method:
The RegExp. Prototype. AutoExec = function (STR) {/ / defined in regular class / / this is refers to the currently executing autoExec method of regular instance if (this. Global) {/ / must be set modifier g this.lastIndex=0; Var a=[]; var a=[]; // Prepare an array to hold each captured result. var result=null; /* // use a while loop. While (result=this.exec(STR)){// if (result=this.exec(STR)){// if (result=this.exec(STR)){// if (result=this.exec(STR)){// if (result=this.exec(STR)){// if (result=this.exec(STR)); } */ do{// The second method uses the do-while loop and evaluates lastIndex. Can you understand that? a.push(this.exec(str)) }while(this.lastIndex); return a; }else{throw new Error(" not set modifier g"); }} // In the STR string, all the complete time string and year, month, day, hour, minute, second are extracted. var str="times is 2013-11-2 12:03:36 ; times is 1998-10-12 3:03:36; times is 2012-11-03 12:22:34"; Var reg = (\ d {4})/(\ d {1, 2}) - (\ d {1, 2}) + (\ d | [01] \ | d [2] [0, 3]) : (\ d | \ [0 to 5] d) : ([0 to 5] \ | d \ d)/g; var r=reg.autoExec(str); Alert (r) // The result is a two-dimensional array like this: [[the 2013-11-2 12:03:36 ""," 2013 ", "11", "2", "12", "3", "36"], [" 3:03:36 1998-10-12 ", "1998", "10", "12", "3", "3", "36"]. 12:22:34 [" 2012-11-03 ", "2012", "11", "3", "12", "22", "34"]]Copy the code

The test method

The test method simply checks to see if STR matches and returns a Boolean value indicating success. Var reg = /b/; Var STR = 'BBS. Zhufengpeixun. Cn "; alert(reg.test(str)); Success, print true.Copy the code

Example 2

var reg = /9/; var str = 'www.zhufengpeixun.cn'; alert(reg.test(str)); On failure, print false.Copy the code