In JavaScript code, using regular expressions for pattern matching often uses some methods of String object and RegExp object, such as replace, match, search and so on. The following is a summary of some methods.
There are four ways to support regular expressions in String objects: search, replace, match, split
str.search(regexp)
Definition: the search() method will retrieve the string matching the expression regexp in string str and return the position of the first character of the first matching string. Returns - 1 if no matching string is found.
For example:
var str = "Javascript"; str.search(/script/); // The position of s in the return script is 4 str.search(/j/i); // Set regular expression to identify i: ignore case, match to J, return position 0
However, the search() method does not support global search because it ignores the identification g of the regular expression parameter and the lastIndex property of regexp, which is always retrieved from the beginning of the string, so it always returns the first matching position of str.
For example:
var str = "javascript is cool"; str.search(/s/g); // Return s position 4 in javascript, and will not continue to retrieve s
str.replace(regexp, replacement)
Definition: the replace() method is an operation to find and replace. It matches the regular expression regexp to the string, and then replaces the string with the replacement string. If there is a global identifier g, it replaces all matching strings, otherwise it only replaces the first matching string.
replace method should be a common method, which is very useful in string replacement operation. For example:
1. Simple replacement
var str = "javascript"; str.replace(/javascript/,'JavaScript'); // Replace string JavaScript with JavaScript
2. Global replace
// Use global representation g for global substitution var str = "javascript"; str.replace(/a/g, 'b'); // Replace all letters a with b, and return jbvbscript
3. Use the special character replacement in the replacement. The $character has special meaning in the replacement. The specific instructions are as follows:
character | replace text |
---|---|
$1,$2,...,$99 | Text matching the first to 99 subexpressions in regexp |
$& | String matching regexp |
$` | Text to the left of the match substring |
$' | Text to the right of the match substring |
$$ | Direct quantity sign |
Here's an example:
//1. Replace with subexpression: $1, $2, etc var str = "javascript"; str.replace(/(java)(script)/,'$2$1'); // In the expression () is a sub expression, and $1 corresponds to the content of the first expression, that is, java, and $2 is script, so the replacement result is: scriptjava //2. $& string matching for positive expression var str = "javascript"; str.replace(/java/,'$&-'); // Regular expressions are matched by directly measuring Java. If the matching result is Java, then the value of $& is Java. Then the string $& - is used to replace the matching string. The result is java script // 3, $` $' $$ var str = "javascript"; str.replace(/ava/,"$`"); // $'is the left text of the matching substring ava, then it is j, then the result of ava after replacement is: jjscript str.replace(/ava/,"$'"); // $'is the right text of the matching substring ava. If it is script, the result of replacing ava is jsscriptscript str.replace(/ava/,"$$"); // $$is a direct quantity symbol, that is, insert a $symbol, and the replacement result is: j$script
4. Use replacement as function replacement
The replacement parameter can be a function rather than a string, which is called for each match, and the string it returns will be used as the replacement text. The first parameter of this function is the string matching the whole pattern, and the next parameter is the string matching the subexpression in the pattern, which can have 0 or more parameters. The next argument is an integer that declares where the match appears in str. The final parameter is STR itself.
Here's an example:
// Match is to match the whole string, that is, 'abc12345 × $*%' // p1 is the first subexpression, ([^ \ d] *), which matches 0 or more non numeric characters, that is, abc // p2 is the second subexpression, (\ d *), matching 0 or more numbers, that is: 12345 // p3 is the third subexpression, ([^ \ w] *), matching 0 or any non word characters. Equivalent to '[^ A-Za-z0-9',, i.e., $*% // offset is the position where pattern matching occurs. If the first character has been successfully matched, the position is 0 // String is the string itself, i.e. 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 parameter is a regular expression or a regular expression created through the RegExp() constructor. The return value is an array containing the matching results.
The regexp regular expression in the match() method is generally divided into two situations: with global flag g set and without global flag g set
1. Set 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 matching var str = "1 plus 2 equals 3"; str.match(/\d/g); // Matches all the numbers that appear in the string and returns an array: [1,2,3]
2. Global flag not set
If the global flag is not set, it is not a global search, just the first match. In this case, the match() method also returns an array. The first element of the array is the matching string, and the remaining elements are the subexpressions enclosed by brackets in the regular expression. Here's an example:
// Non global match 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 regular expression matching is: http://www.example.com // First subexpression (\ w) match result: http // The second subexpression ([\ w.]) matches: www.example.com
str.split(delimiter, limit)
Definition: the split() method can decompose the calling string into a string array, and the separator used is its parameter.
Parameters:
delimiter: a string or regular expression that splits a string from the place specified by this parameter.
limit: Specifies the maximum length of the returned array. If this parameter is not set, the entire string will be split.
For example:
//1. Only one parameter is passed, and the whole string is divided by default var str ="a,b,c,d,e"; str.split(','); // Returns the split string array: ["a", "b", "c", "d", "e"] //2. Two parameters passed in var str ="a,b,c,d,e"; str.split(',',3); //Specify the qualified length to return the corresponding array: ["a", "b", "c"] //3. Use regular expression matching without split string var str = "aa44bb55cc66dd"; str.split(/\d /); //Split the string by matching numbers, but not including the split string, then the returned result is: ["aa","bb","cc","dd"]; //4. Use regular expression matching, including split strings var str = "aa44bb55cc66dd"; str.split(/(\d )/); //Split the string by matching numbers, and if the split string is contained in a subexpression, the returned result is: ["aa", "44", "bb", "55", "cc", "66", "dd"]
Above.