Home > Back-end >  JavaScript regular expressions
JavaScript regular expressions

Time:01-22

- a regular expression: also known as the regular expression
- English named Regular Expression in the code, often abbreviated to regex, RegExp or RE
- define some rules of the string, conform to the rules are extracted from the content of the
1, the search ()
This method is the most simple way, it with regular expressions as parameters, return the first substring matched the beginning of the location of the characters, if no match, return 1, return 1
First, if the parameters of the search is not regular, it will first be converted to regular, second, the search is not support global search, so you don't bother with it to search the global
2, the replace ()
The method to perform a search and replace operation, the first parameter is the regular expression, the second parameter is to replace the string or operation function, when the second parameter is a function processing, processing function corresponding parameters of the matching substring, respectively capture grouping, subscript, a copy of the string


3, the match ()
This method is more commonly used method in the String, it is the only parameter is the regular expression, return the array contains the matching results, the method to support global match,

If you have g logo, return is an array containing all of the matching, then capture group is invalid; On the other hand, there is no g logo, return or an array, the array [0] is a full match, array [n] is $n capture (premise is you have captured packet), when there is no g logo, will return to the match array has 2 extra attributes - the index and the input, the index matching position, needless to say the input is a copy of the target string,
4, the split ()
This method has two parameters, the second parameter is the limit the length of the array, if specified, don't more than the length of elements in the array,
  • Related