directory
1. The match () method
Grammar: stringobj. Match (rgExp)
Example:
2. The search () method
Grammar: stringobj. Search (rgExp)
Example:
3. The replace () method
Grammar: replace (rgExp replaceText)
Example:
4. The split () method
Grammar: the split ([the separator [limit]])
1. The match () method
The match () method uses the regular expression pattern to find the string and returns the results containing the lookup as an array.
Grammar: stringobj. Match (rgExp)
Stringobj: Mandatory. The String or String literal on which to look up
RgExp: Mandatory. Regular expression objects for regular expression patterns and available flags. It can also be a variable name or string literal that contains regular expression patterns and available flags.
If the match method does not find a match, null is returned. If a match is found, an array is returned and the properties of the global RegExp object are updated to reflect the result.
The match method returns an array with three attributes: input, index, and lastIndex.
If there is no global flag (g), element 0 of the array contains the entire match, and elements 1-n contain any child matches that have ever occurred in the match. This is equivalent to no global flag being set, and elements 0-n contain all matches.
Example:
<script language="JavaScript">
function MatchDemo(){
var r,re; // Declare variables
var s="I'm a good man";
re=/man/i; // Create a regular expression
r=s.match(re); // Try to match the search string
return(r); // Return to the first occurrence of "body"
}
document.write(MatchDemo());
</script>
Copy the code
<script language="JavaScript">
function MatchDemo(){
var r,re; // Declare variables
var s="I'm a man a good man";
re=/man/ig; // Create a regular expression
r=s.match(re); // Try to match the search string
return(r); // Return to the first occurrence of "body"
}
document.write(MatchDemo());
</script>
Copy the code
2. The search () method
The search() method returns the position of the first substring that matches the regular expression lookup.
Grammar: stringobj. Search (rgExp)
Stringobj: Mandatory. The String or String literal on which to look up
RgExp: Mandatory. Regular expression objects for regular expression patterns and available flags. It can also be a variable name or string literal that contains regular expression patterns and available flags.
Example:
<script language="JavaScript">
function MatchDemo(){
var r,re; // Declare variables
var s="I'm a man a good man";
re=/man/ig; // Create a regular expression
r=s.search(re); // Try to match the search string
return(r); // Return to the first occurrence of "body"
}
document.write(MatchDemo());
</script>
Copy the code
3. The replace () method
The replace() method searches the string using an expression pattern and replaces the searched content with the specified string, returning a string object containing the replaced content.
Grammar: replace (rgExp replaceText)
The rgExp argument is the expression object to use when searching. If it is a string, the search is precise rather than fuzzy as a regular expression.
The replaceText parameter is a string used to replace what was searched, where special character combinations can be used to represent matching variables. Where $& is the string that the entire expression pattern matches in the searched string, $is all the content to the left of the string that the expression pattern matches in the searched string, $’ is all the content to the right of the string that the expression pattern matches in the searched string, and $$is the ordinary meaning of the “$” character.
Example:
<script language="JavaScript">
var strSrc="a13f58af4f41af";
var re=/(\d)(\d)/gi;
var strDest=strSrc.replace(re,"$2 $1");
document.write("String"+strSrc+"Is converted to:"+strDest);
</script>
Copy the code
4. The split () method
The split() method returns an array of substrings produced when a string is split into substrings by some split identifier.
Grammar: the split ([the separator [limit]])
Separator is the split identifier argument, which can be multiple characters or a regular expression and is not returned as part of the array element. The limit argument limits the number of elements returned.
<font size="+ 1"<font face="宋体"> <script language="JavaScript"> var splitArray=new Array(); Var string="JavaScript, ASP, JSP, Java"; Var regex = /, /; splitArray=string.split(regex); for(i=0; i<splitArray.length; i++){ document.write(splitArray[i]+" "); } </script>Copy the code