Regexp in javascript

Source: Internet
Author: User

/* The main method of RegExp object: *********************/
/*
* Exec (): This method is specially designed for the capture group.
@ Param: a string that accepts a parameter and applies the pattern.
@ Return: returns an array containing the first matching item information. If no matching item exists, null is returned.
@ Explain: the returned Array is an Array instance and contains two additional attributes:
Index-indicates the position of the matching item in the string
Input-indicates a string that applies a regular expression.
The first item in the array is the string that matches the entire pattern. The other items are the strings that match the capture group in the pattern. (If NO capture group exists, only one array is returned)
*/
Var text = 'Gid and var and global ';
Var patterns =/gid (and var (and global )?)? /;
Var matches = patterns.exe c (text );
Console. log (matches. index); // 0 the entire string matches the pattern starting from 0, so the index of the returned array matches is 0.
Console. log (matches. input); // "gid and var and global" indicates the string to be matched by the Regular Expression
Console. log (matches [0]); // the first entry in the "gid and var and global" array is the matching string.
Console. log (matches [1]); // "and var and global" (and var (and global )?)? The second item contains the content that matches the first capture group.
Console. log (matches [2]); // "and global" (and global )? The third item contains the content that matches the second capture group.
Console. log (matches [3]); // undefined no third capture group, return undefined
 
// Exec () method. Even if the global flag is set in the mode, only one matching item is returned each time.
// If the global flag is not set, the first matching item is always returned when exec () is called multiple times on the same string,
// When (g) is set and exec () is called multiple times on the same string, the system returns the string to continue searching for new matching items.
// {LastIndex in IE changes every time even in non-g mode .}
 
Var txt = 'cat, bat, sat, fat ';
Var patterns1 =/. /;
Var matches1 = patterns1.exec (txt );
Console. log (matches1.index); // 0 has been matched since the 0 position
Console. log (matches1 [0]); // cat
Console. log (patterns1.lastIndex); // start searching for the string position of the next matching item.
 
Matches1 = patterns1.exec (txt );
Console. log (matches1.index); // 0
Console. log (matches1 [0]); // cat
Console. log (patterns1.lastIndex); // 0
 
Var patterns2. =/. at/g;
Var matches2 = patterns2.exec (txt );
Console. log (matches2.index); // 0
Console. log (matches2 [0]); // cat
Console. log (patterns2.lastIndex); // 3
 
Matches2 = patterns2.exec (txt );
Console. log (matches2.index); // 5
Console. log (matches2 [0]); // bat
Console. log (patterns2.lastIndex); // 8
 
/*
* Test ()
* @ Param: {type: String}
* @ Return: {type: Boolean}
* @ Explains: return true when matching; otherwise, return false,
*/
Var stxt = '000-12-2354 ';
Var patterns3 =/\ d {3}-\ d {2}-\ d {4 }/;
If (patterns3.test (stxt )){
Console. log ("match sucess! ")
}
/*
* ToLocalString () and toString ()
* @ Return: returns the literal form of the regular expression, regardless of the method used to create the regular expression.
*/
Var patterns4 = new RegExp ("\ [bc \] at", "gi ");
Var patterns5 =/\ [bc \] at/gi;
Console. log (patterns4.toLocaleString (), patterns4.toString (); // \ [bc \] at/gi/\ [bc \] at/gi
Console. log (patterns5.toLocaleString (), patterns5.toString (); // \ [bc \] at/gi/\ [bc \] at/gi
 
/***************** RegExp constructor attributes (these attributes are regarded as static attributes in other languages ): *********************/
/*
* Long attributes and short attributes
* Input $ _ string to be matched last time
* LastMatch $ & latest match
* LastParen $ + the last matched capture group
* LeftContext $ 'text before lastMatch in the input string
* Multiline $ * Boolean value, indicating whether all expressions use multiline mode.
* RightContext $ 'text after lastMatch in the input string
* These attributes apply to all regular expressions in the scope www.2cto.com.
* Note: opera does not support the input, lastMatch, lastParen, and multiline attributes.
* IE does not support the multiline attribute.
*/
Var text = 'This has been a short summer ';
Var pattern =/(.) hort/g;
 
If (pattern. test (text )){
Console. log (RegExp. input + ';', 'atattr: '+ RegExp. $ _ +'; '); // this has been a short summer
Console. log (RegExp. leftContext); // "this has been a" matches the text before short
Console. log (RegExp. rightContext); // "summer" matches the text after short
Console. log (RegExp. lastMatch); // "short" last match
Console. log (RegExp. lastParen); // the first character of the last matching capture group (.) is placed in the capture group.
Console. log (RegExp. multiline); // false; multi-row mode not supported
}
 
// Use the short attribute name instead. Because the short attribute name is not a valid ECMAScript identifier, you must use the square brackets syntax to access them.
Var text1 = 'This has been a short summer ';
Var pattern1 =/(.) hort/g;
If (pattern1.test (text1 )){
Console. log (RegExp ["$ _"]); // "this has been a short summer"
Console. log (RegExp ["$ '"]); // "this has been"
Console. log (RegExp ["$ '"]); // "summer"
Console. log (RegExp ["$ &"]); // short
Console. log (RegExp ["$ +"]); // the first character of the most recent matching capture group (.) is placed in the capture group
Console. log (RegExp ["$ *"]); // false
}
/*
* In addition to the preceding attributes, there are as many as nine constructor attributes used to store the capture group.
* RegExp. $1, RegExp. $2... RegExp. $9
* Store the first, second, and ninth matching capturing groups respectively. These attributes are automatically filled when the exec () and test () methods are called.
* Even if test () is called to return a Boolean value, the RegExp Constructor's attribute $1 and $2 will be automatically filled.
*/
Var text2 = 'This has been a short communication'
Var pattern2 =/(...) ic (...) o (.)/;
If (pattern2.test (text2 )){
Console. log (RegExp. $1); // un
Console. log (RegExp. $2); // ati
Console. log (RegExp. $3); // n
}
 
/******************************** String pattern matching method *** *******************/
/*
* Match ()
@ Param: a parameter, such as a regular expression or RegExp object.
@ Return: returns an array, which is similar to exec.
* Search (regexp)
@ Param: Same as the match () parameter.
@ Return: Start position of the first substring in the string that matches regexp. Returns the first matched index. If no matching index is found,-1 is returned;
@ Explain: always search back from the beginning of the string
* Replace (regexp/string, string/function)
@ Param: the first parameter is a RegExp object or a string (the string is not converted to a regular expression). (which mode or string is used to replace it)
@ Param: The second parameter can be a string or a function (replaced content)
@ Return: A New string, which is obtained after the first match or all matches of regexp/string are replaced by string/function.
@ Explain: to replace all substrings, you must provide a regular expression and set global (g)
@ Extra: If the second parameter is a string, you can use a Character Sequence (short attribute name) to insert the value obtained by the regular expression operation to the result string.
Split (string/regexp, Array. length)
@ Param: the first parameter can be a separator, which can be a string or a regexp object.
@ Param: Specifies the length of the array so that the returned array does not exceed the specified length.
@ Return: array returns a matched array.
*/
Var text = 'cat, bat, sat, fat ';
Var pattern =/. /;
// Same as pattern.exe c (text)
Var matches = text. match (pattern );
Console. log (matches. index); // 0
Console. log (matches [0]); // cat
Console. log (pattern. lastIndex); // 0
 
Var text1 = 'sst, bat, sat, fat ';
Var pos = text1.search (//);
Var pos1 = text1.search (//);
Console. log (pos, pos1); // 6 6 6 matches bat
 
 
Var result = text. replace ('at', 'oncat ');
Console. log (result); // concat, bat, sat, fat
Var result1 = text. replace (/at/g, 'fs ');
Console. log (result1); // cfs, bfs, sfs, ffs
 
// The second parameter is string; $1, the first entry of the capture group, that is, the matching result of (.)
Var result2 = text. replace (/(. at)/g, 'Fuck [$1] ');
Console. log (result2); // fuck [cat], fuck [bat], fuck [sat], fuck [fat]
 
Var colorText = "red, blue, green, yellow ";
Var r1 = colorText. split (",");
Var r2 = colorText. split (",", 2 );
Var r3 = colorText. split (/[^ \,] + /);
Console. log (r1); // ["red", "blue", "green", "yellow"]
Console. log (r2); // ["red", "blue"]
Console. log (r3); // ["", ""]
 

From Jalen

Related Article

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.