http://web.jobbole.com/85433/@ Bole Online-Fire of Liaoyuan
Using regular expressions for pattern matching in JavaScript code often uses several methods of string objects and RegExp objects, such as replace, match, search, and the following is a summary of some of the methods used.
There are 4 ways to support regular expressions in a string object, namely: Search, replace, match, split
Str.search (RegExp)
Definition: The search () method retrieves a string that matches the expression regexp in Str, and returns the position of the first character of the first matching string. If no matching string is found, 1 is returned.
For example:
var str = "Javascript"
str. Search (/script/< Span class= "Crayon-sy"); //returns the position of S in Script 4
str. Search (/j/i); //set regular expression is ID I: Ignore case, Match to J, return position 0
However, the search () method does not support global search because it ignores the identity g of the regular expression parameter and ignores the Lastindex property of RegExp, always retrieving from the beginning of the string, so it always returns to the first matching position of Str.
For example:
var str = "JavaScript is cool";
Str. Search(/s/g); //Returns the S position in JavaScript 4, does not continue retrieving s
Str.replace (RegExp, replacement)
Definition: The Replace () method is the operation to perform a find and replace. It regexp the regular expression to the string that matches it, and then replaces it with the strings of replacement, and if there is a global identity G, replaces all matching strings, or replaces only the first matching string.
The Replace method should be a more common method that is useful in string substitution operations. For example:
1. Simple replacement
var str = "JavaScript";
Str. Replace(/javascript/,' JavaScript '); //Replace the string JavaScript with JavaScript
2. Global substitution
Global substitution with global representation of G
var str = "JavaScript";
Str. Replace(/a/g, ' B '); //Replace all letters A with the letter B and return to Jbvbscript
3, the use of special characters in replacement replacement, in the replacement $ character has a special meaning, the following table is specified:
character |
Replace text |
$, $ 、...、 |
Text that matches the 1th to 99 sub-expressions in RegExp |
$& |
Strings that match regexp |
$` |
Text located to the left of the matching substring |
$ |
Text on the right side of the matching substring |
$$ |
Direct volume symbol |
Take a look at the example:
1. Replace with sub-expression: $ $, $ $, etc.
var str = "JavaScript";
Str. Replace(/(java) (script)/,' $2$1 ');
The //Expression () is a sub-expression, which corresponds to the contents of the first expression, that is, java,$2 is a script, so the result of the substitution is: Scriptjava
2. $& a string that matches a positive expression
var str = "JavaScript";
Str. Replace(/java/,' $&-');
//The regular expression is matched by a direct amount of Java, the match result is Java, then the value of $& is Java, then the string $&-to replace the matched string, the result is Java-script
3, $ ' $ ' $$
var str = "JavaScript";
Str. Replace(/Ava/,"$ '"); //$ ' for the left text of the matched substring Ava, then J, then the result of the replacement Ava is: Jjscript
Str. Replace(/Ava/,"$ '"); //$ ' to match the right text of the substring Ava, or script, the result of replacing Ava is: Jscriptscript
Str. Replace(/Ava/,"$$"); //$$ is a direct volume symbol, that is, insert a $ symbol and replace the result with: J$script
4. Replace with replacement as function
The parameter replacement of replace can be a function instead of a string, and each match invokes the function, and the string it returns is used as the replacement text. The first parameter of the function is a string of the entire matching pattern, and the next argument is a string that matches the subexpression in the pattern, which can have 0 or more parameters. The next parameter is an integer that declares where the match appears in Str. The final parameter is STR itself.
Let's look at an example:
Match matches the entire string, that is: ' abc12345#$*% '
P1 is the first subexpression, ([^d]*), matches 0 or more non-numeric characters, namely: ABC
P2 is the second subexpression, (d*), matching 0 or more digits, that is: 12345
P3 is the third subexpression, ([^w]*), matches 0 or matches any non-word character. Equivalent to ' [^a-za-z0-9_] ', i.e. #$*%
Offset is the position where the pattern match occurred, from the first character has been successfully matched, the position is 0
The string itself, that is, the abc12345#$*%
function replacer(match, p1, p2, P3, offset , string) {
return [p1, p2, P3]. Join('-');
}
var newstring = ' abc12345#$*% '. Replace(/([^D]*) (d*) ([^W]*)/ , replacer);
The replacement result is: abc-12345-#$*%
Str.match (RegExp)
Definition: the match () method is the most commonly used string regular expression method. Its only argument is a regular expression or a regular expression created by the RegExp () constructor, which is an array that contains the matching results.
The regexp regular expression in the match () method is generally divided into two cases: the global flag G is set and the global flag G is not set
1, have set the global flag
If the global flag G is set, the returned array contains all the matching results that appear in the string, for example:
Global match
var str = "1 plus 2 equals 3";
Str. Match(/D/g); //matches all occurrences of the number in the string and returns an array: [All-in-one]
2. No global flag set
If the global flag is not set, it is not a global retrieval, but the first match is retrieved. In this case, the match () method matches the result of returning an array, the first element of the array is a matching string, and the remaining element is a subexpression enclosed in parentheses in the regular expression. Let's look at an example:
//non-global matching
var str = "Visit my blog at http://www.example.com";
Str. Match(/(w+)://([w.] +)/); Results returned: ["http://www.example.com", "http", "www.example.com"]
The result of the match between//regular expression is: http://www.example.com
///First sub-expression (w+) matches result: http
Second sub-expression ([w.]) Matching results: www.example.com
Str.split (delimiter, limit)
Definition: The Split () method decomposes the string that invokes it into an array of strings, using the delimiter as its argument.
Parameters:
Delimiter: A string or regular expression that splits a string from the location specified by the parameter.
Limit: Specifies the maximum length of the returned array, and if the parameter is not set, the entire string will be split.
For example:
1. Only one parameter is passed, the entire string is split by default
var str ="A,b,c,d,e";
Str. Split(', '); //Returns a segmented array of strings: ["A", "B", "C", "D", "E"]
2. Pass in two parameters
var str ="A,b,c,d,e";
Str. Split(', ',3); //Specify a qualified length, the corresponding array is returned: ["a", "B", "C"]
3. Use regular expression matching, do not include the split string
var str = "AA44BB55CC66DD";
Str. Split(/D+/); //The string is split by matching numbers, but does not contain a split string, the result is: ["AA", "BB", "CC", "DD"];
4. Use regular expression matching, including split string
var str = "AA44BB55CC66DD";
Str. Split(/(D+)/); //Divide the string by matching numbers, and the split string is contained in the subexpression, the result is: ["AA", "" "", "" BB "," "", "" CC "," zero "," DD "]
Above.
A simple analysis of the String method of regular expression pattern matching