Regular expressions, also known as regular representations and regular representations. (English: Regular Expression, often abbreviated as regex, regexp or RE in code), a concept of computer science. Regular expressions use a single string to describe and match a series of syntactic rules. In many text editors, regular expressions are usually used to retrieve and replace text that conforms to a certain pattern.
1. Regular expressions in Javascript
In Javascript, regular expressions can be constructed using RegExp objects. We need to create a new instantiated RegExp() object, and we can pass in two parameters: the first parameter is the matching pattern, and the second parameter is an optional parameter, and we can pass in three parameters. i means case insensitive, g means global matching, that is, matching all strings that meet the conditions, m means performing multiple matches. Examples are as follows:
var reg = new RegExp("Hello", "i"); // means that the Hello string in the string is matched and is case-insensitive.
2. Use exec for pattern matching
There is a method in RegExp that can perform pattern matching and return the result: exec(). This method is very important, basically a function that is necessary to use JS for pattern matching. However, many people don’t know the return value of this function, so errors often occur when actually using it. Here we systematically introduce some methods of using exec().
The basic format of exec() is: (string), where RegExpObject is the set regular matching object and string is the string to be matched. If the match is successful, an array is returned; if there is no successful matched part of the string, null is returned.
The point here is this array. What exactly does the array return? You can take a look at the following experiment.
var re = new RegExp("[?#&]" + user + "=([^&#]*)", "i")
This code matches a url and can be used to obtain the parameter part after user=. So if you use a url and use this pattern to perform exec operation, what will be returned? For example, we have the following
?user=Tom&psw=123456
The result of the array returned by exec is: [?user=Tom, Tom]. You can see that the first element of the return array is the string matched by the entire matching pattern, and the second matching character happens to be the parameter value.
This is the rule returned by exec matching: the first element is the entire matching string, and starts from the second parameter to return the string matched by the grouping defined by each () in the pattern.
What ([^&#]*) returns is a string that does not start with & or #, that is, the corresponding parameters followed.
If we modify the defined pattern to [?#&]" + (user) + "=([^&#]*), then the array returned after exec() is [?user=Tom, user, Tom].
The above is what the editor introduced to you using the exec() method in JS to construct regular expression verification. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!