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 - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- i want to call a child component method from a parent in vuejs
- xcode - pod install [!] no `podfile 'found in the project directory
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file