Regular expression,Also known as regular notation, regular notation.(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 rules that match a certain syntax.In many text editors,Regular expressions are often used to retrieve and replace text that matches a pattern.
var reg=new regexp ("hello", "i");//Matches the hello string in the string and is not case sensitive.
Second, use exec for pattern matching
There is a method in regexp that performs pattern matching and returns the result:exec (). This method is very important.Basically, the necessary function for pattern matching using js.However, the return value of this function is not clear to many people.Therefore, in actual use, errors often occur.Here is a systematic introduction to the use of exec ().
The basic format of exec () is:regexpobject.exec (string), where regexpobject is a set regular matching object.string is the string to be matched.If it matches successfully,Returns an array;If there is no string part that successfully matches,It returns null.
The point here is this array.What exactly does the array return?Take a look at the following experiment.
var re=new regexp ("[?#&]" + user + "= ([^] *)", "i")
This code performs a url match, which can be used to get the parameter part after user =,So if you use a url and use this mode for exec operation, what will the result return?For example we have the following
The result of the array returned by exec is:[?user=tom, tom]. You can see that the first element of the returned array is the string matched by the entire matching pattern.The second matched character happens to be the parameter value.
This is the rule returned by exec matching:the first element is the entire matched string,Starting from the second parameter, the string matched by each group defined by () in the pattern is returned.
This ([^] *) returns a string that does not start with&or #,That is the corresponding parameter later.
If we modify the defined pattern to [?#&] "+ (user) +"=([^] *), then the array returned after exec () is [?user=tom , user, tom].
- Replace parentheses, angle brackets, etc with JS regular expressions
- JS method of filtering multiple words using regular expressions and replacing them with asterisks of the same length
- On the use of RegExp objects and parentheses in JS regular expressions
- ExtJS4 text field (textField) method using regular expressions for validation (Regex)
- JS method to remove duplicate characters in a string using regular expressions
- python 3x - typeerror: 'method' object is not subscriptable
- python - you may need to restart the kernel to use updated packages error
- xcode - pod install [!] no `podfile 'found in the project directory
- android studio - unresolved reference comes out in kotlin
- android studio - emulator: dsound: could not initialize about the error message directsoundcapture
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- django - oserror: [winerror 123] the file name, directory name, or volume label syntax is incorrect : '<frozen importlib_boot
- mysql startup failed [error] innodb: the innodb_system data file 'ibdata1' must be writable
- python - importerror: cannot import name md5 error cannot be resolved