[Unsystematic memo] Full contact with JS Regular Expressions

Source: Internet
Author: User
ArticleDirectory
    • The regular expression can be:
    • Modifier
The regular expression can be:

• 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.

Modifier
Modifier Description
I Perform case-insensitive matching.
G Perform global match (search for all matches instead of stopping the first match ).
M Perform multi-row matching.

Other memos see http://www.w3school.com.cn/js/jsref_obj_regexp.asp

Http://www.iteye.com/topic/481228

 

We will focus on analyzing some methods of JS Regular Expressions and their usage

Reg. Test (STR)
STR ="Certainly! He loves her! ";
Alert (Re. Test (STR ));//True, As long as it contains HE (HE), it will match. To only contain he or he, there cannot be other characters, you can use ^ and $

Exec and match
 
/* Regexpobject.exe C (string)
Returns an array containing matching results. If no match is found, the return value is null.
 
*/
 
.VaRSometext ="Web2.0. net2.0";
VaRPattern =/(\ W +) (\ D) \. (\ D )/;
 
VaROutcome_exec = pattern.exe C (sometext );

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

1. Always match

2. The returned array is the first matching result and a collection of group matching results.

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

 
VaRSometext ="Web2.0. net2.0";
 
VaRPattern =/(\ W +) (\ D) \. (\ D )/;
 
VaROutcome_matc = sometext. Match (pattern );// Return values similar to arrays [, web,]
 
VaRPattern2 =/(\ W +) (\ D) \. (\ D)/g;
 
VaROutcome_matc2 = sometext. Match (pattern2); // return values similar to arrays [Web2.0, net2.0]

1. If no global variable exists, match is the same as exec.

2. If there is a global variable, match only returns the result set of the regular expression matching, and does not contain the results captured by any group.

PS: \ W is used. \ W is equivalent to [0-9a-za-z]. However, in the above expression (\ W +), it only matches the web, but does not match web2.

This is because if \ W is followed by \ D, \ W is only equivalent to [A-Za-Z]

Search, split, replace

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.