JavaScript supports regular expressions through the RegExp class. The simplest example is as follows:
Copy codeThe Code is as follows:
Var regApple = new RegExp ("apple ");
It can match the first "apple" string in a string and is case sensitive. When the second parameter "g" is added to the constructor, all "apple" in the string is searched. Here "g" stands for "global ". If the second parameter is "I", it indicates case-insensitive. The matching process will not consider the case of letters. By combining the two, you can search all the "apple" strings, regardless of the case.
Copy codeThe Code is as follows:
Var regApple = new RegExp ("apple", "gi ");
A regular expression has a non-unique expression method. The preceding expression can be expressed as follows using the syntax in Perl:
Copy codeThe Code is as follows:
Var regApple =/apple/gi;
After a RegExp object is created, the RegExp method can construct different matching methods, because the regular expression is used to operate strings, therefore, some methods of String play an important role in constructing regular expressions.
RegExp object Method
Copy codeThe Code is as follows:
Var sampleString = "Greenapple ";
Var regApple =/apple /;
Alert (regApple. test (sampleString ));
The output result of the above code is "true", because sampleString contains the string to be matched "apple", which is the simplest way to detect. Sometimes, we need to know the detailed matching results, for example:
Copy codeThe Code is as follows:
Var sampleString = "green apples, red apples ";
Var regApple =/apple/g;
Var arr = regApple.exe c (sampleString );
By using the exec () method, the returned arr is an array of matching results, including each matched value and its segment, for example, in the above example, "green apples" or "red apples ". The match () method has the same function as exec (), but its expression is different:
Copy codeThe Code is as follows:
Var sampleString = "green apples, red apples ";
Var regApple =/apple/g;
Var arr = sampleString. match (regApple );
The search () method is similar to indexOf (), and returns the location of the first matched string:
Copy codeThe Code is as follows:
Var sampleString = "green apples, red apples ";
Var regApple =/apple/gi;
Alert (sampleString. search (regApple); // output "6"
String Method
The String replace () method can replace the specified String with another String:
Copy codeThe Code is as follows:
Var sampleString = "There is a green apple .";
Alert (sampleString. replace ("green", "red"); // output "There is a red apple ."
Replace the first parameter of replace () with a regular expression to achieve the same effect:
Copy codeThe Code is as follows:
Var sampleString = "There is a green apple .";
Var regApple =/apple /;
Alert (sampleString. replace (regApple, "red"); // output "There is a red apple ."
The second parameter of replace () can be replaced with a function (). This function () accepts a matching string as the parameter and returns a replacement string. (In doubt)
The regular expression can be used to implement the same split () function of String.
Copy codeThe Code is as follows:
Var fruit = "apple, pear, lemon ";
Var arr = fruit. split (",");
Use a regular expression:
Copy codeThe Code is as follows:
Var fruit = "apple, pear, lemon ";
Var reg = /\,/;
Var arr = fruit. split (reg );