• Test a mode of a string. For example, you can test an input string to see if there is a phone number or a credit card number. This is called Data Validity verification.
• Replace text. You can use a regular expression in a document to identify a specific text, and then delete it all or replace it with another text.
• Extract a substring from the string based on the pattern match. It can be used to search for specific text in text or input fields.
We will focus on analyzing some methods of JS Regular Expressions and their usage
The outcome_exec value is [Web2.0, web, 2, 0]. this is an array. if the expression 'pattern' is followed by a global G. the result is [Web2.0, web, 2, 0]. visible Exec
If the regular expression does not have a global variable, String. Match returns the same result as exec. If G exists, match does not return the results captured by the group. For example
Search returns the start index of the matching expression. The global variable is invalid.-1 is not found.
STR ="My age is 18. Golden Age! ";// The age is not certain. We cannot find its location using indexof.
Re =/\ D + /;
Alert (Str. Search (re ));// Return the searched string start subscript 10
// The followingCodeAlthough there is no error, the G mark is redundant.
Re =/\ D +/g;
Alert (Str. Search (re); // still 10
Others
STR ="Some some \ tsome \ t \ f";
Re =/\ s + /;
Alert (Str. Replace (Re,"#"));// But this will only Replace the first pile of blank characters
// Because a regular expression can only be matched once, \ s + will exit after matching the first space.
Re =/\ s +/g;// G, global flag, will make the regular expression match the entire string
Alert (Str. Replace (Re,"@"));// Some @
// The other is similar to the split
VaRSTR ="A-BD-c";
VaRArr = Str. Split ("-");// Return ["A", "BD", "C"]
// If STR is input by the user, it may input a-BD-C or a bd c or a_bd_c, but not ABDC (in this case, it is wrong)
STR =A_db-c";// The user adds the separator s as he prefers.
Re =/[^ A-Z]/I;// As we mentioned earlier, ^ indicates the start of a character, but in [] it indicates a negative character set
// Match any character that is not within the specified range. All characters except letters are matched here.
Arr = Str. Split (re); // still return ["","BD","C"];
Complex replace applications
// Calculate the number of times a string has exceeded 9
VaR STR ="Adf9df9df9",// The string in the text file;
Re =/9/GI,// Match 9
Counter = 0;// Counter
Str. Replace (Re, function (){
Counter ++;// Every time a match occurs, the function is executed. The return value of the function is used to replace the original value.
Return "#";
});
// Finally, STR is changed to ADF # DF #"
FunctionF2c (s ){
VaRTest =/(\ D + (\. \ D *)?) F \ B/g;// Initialization mode.
Return(S. Replace (test,Function($0, $1, $2 ){
Return($1-32) * 5/9) +"C");
}));
}
Document. Write (f2c ("Water freezes at 32f and boils at 212f ."));
// Water freezes at 0C and boils at 100C.
PS: when the second parameter in the replace method is a function, $0, $1... is included in the function ..... $ N represents the subcapture of a regular expression.
Replace does not update the global attribute of Regexp.