http://louiszhai.github.io/2016/06/13/regexp/# regular expression classification

Regular expressions play an important role in many programming languages. Learning to establish regular expressions can get twice the result with half the effort in data processing

The pattern modifier of a regular expression

– I Ignores case and case. G Global match. M Multi-line match

1. Two creation modes are available

var  box =   new RegExp("box"."i");
var box =   /box/i;Copy the code

2.RegExp object methods

– exec a RegExp method that performs a search for a match in a string, which returns an array (null if no match is found) test a RegExp method that tests a match in a string, which returns true or false. Match A String method that performs a search for a match in a String, returning either an array or null if there is no match. Search A String method that tests a match in a String, returning the index of the matched position, or -1 on failure. Replace A String method that looks for a match in a String and replaces the matched substring with a replacement String. Split a String using a regular expression or a fixed String, and stores the delimited substrings into an array of String methods

Sample test method using the new operator

var pattern = new RegExp('box'.'i');
var str = 'This is a Box! ';
alert(pattern.test(str));Copy the code

An example of the test method using a literal approach

var pattern = /box/i;
var str = 'This is a Box! ';
alert(pattern.test(str));Copy the code

Use a single statement to implement a regular match

alert(/box/i.test('This is a Box! ')); // Mode and string replace two variablesCopy the code

Use exec to return the matching array

var pattern = /box/i;
var str = 'This is a Box! '; alert(pattern.exec(str)); // Return array if match, otherwise return nullCopy the code

Get an array of matches using the match method

var pattern = /box/ig;
var str = 'This is a Box! ,That is a Box too'; alert(str.match(pattern)); alert(str.match(pattern).length); // find the return position, otherwise return -1 PS: because the search method found on the return, that is, no g globalCopy the code

Use search to find matching data

var pattern = /box/ig;
var str = 'This is a Box! ,That is a Box too';
alert(str.search(pattern));Copy the code

Replace the matched data with replace

var pattern = /box/ig;
var str = 'This is a Box! ,That is a Box too';
alert(str.replace(pattern, 'Tom')); // Replace Box with TomCopy the code

Split into string arrays using split

var pattern = / /ig;
var str = 'This is a Box! ,That is a Box too'; alert(str.split(pattern)); // Split whitespace into arraysCopy the code

Character classes

– Metacharacter/metacharacter matching. Matches any character except newline. Matches any character in parentheses. [^ A-z0-9] Matches any character in parentheses. \d matches digits \W matches non-alphanumeric and underscore _ \ O matches null character \b matches space character \ F matches base character \n matches newline character \ R matches carriage return character \t matches TAB character \s matches whitespace character. The blank space. The TAB and newline characters \S match non-whitespace characters ^ the beginning of the line matches $the end of the line matches \A only matches the beginning of the string \b matches the word boundary, \B matches non-word boundaries \G matches at the beginning of the current search \Z matches at the end of the string or line. \ this\where matches this or any of these

– Record matching case \1 or $1 matches the first group or contents in the first group \1 or $1 matches the contents in the first group? Matches 0 or 1 * Matches 0 or any number of + Matches at least one (xyz)+ matches at least one (xyz) x{m,n} Matches at least m x and at most n x

Greed and inertia are the greed card followed by? No.

– Greed and inertia ++? {n,m}{n,m}?

Use dot metacharacters

var pattern = /g.. gle/; var str ='google';
alert(pattern.test(str));Copy the code

Repeat matching

var pattern = /g.*gle/;
var str = 'google';
alert(pattern.test(str));Copy the code

Use character class matching

var pattern = /g[a-zA-Z_]*gle/;
var str = 'google';
alert(pattern.test(str));Copy the code

Use anchor metacharacter matching

var pattern = /^google$/;
var str = 'google';
alert(pattern.test(str));Copy the code

/^ matches from the beginning,$matches from the end

var pattern = /goo\sgle/;
var str = 'goo gle'; alert(pattern.test(str)); //\s can match the space var pattern = / Google \b/; var str ='google'; alert(pattern.test(str)); //\b can match if the boundary is reachedCopy the code

Use or pattern matching

var pattern = /google|baidu|bing/;
var str = 'google';
alert(pattern.test(str));Copy the code

Use packet pattern matching

Var pattern = {4, 8} (Google) /; var str ='googlegoogle'; alert(pattern.test(str)); // Matches the string in the group 4-8 timesCopy the code
var pattern = /8(.*)8/; / / get 8.. Any character between 8 var STR ='This is 8google8';
str.match(pattern);
alert(RegExp.The $1); / / get 8.. Any character between 8Copy the code

greed

var pattern = /8(.*)8/;
var str = 'This is 8google8';
var result = str.replace(pattern,'<strong>$1</strong>'); Document.write (result);Copy the code
var pattern = /(.*)\s(.*)/;
var str = 'google baidu';
var result = str.replace(pattern, '$2 $1'); // Replace the values of the two groups with the output document.write(result);Copy the code

Use forward-looking capture

var pattern = /(goo(? =gle))/; //goo must be followed by gle to catch var STR ='google';
alert(pattern.exec(str));Copy the code

Use group nesting

var pattern = /(A? (B? (C?) )) /; // Get var STR = from the outside'ABC';
alert(pattern.exec(str));  //abc,abc,bc,aCopy the code

1. Check the zip code

var pattern = /[1-9][0-9]{5}/; // The first digit cannot be 0. Var STR ='224000';
alert(pattern.test(str));Copy the code

2. Check the compressed file package

var pattern = /[\w]+\.zip|rar|gz/; //\w indicates all numbers and letters underlined var STR ='123.zip'; Alert (pattern. Test (STR)); //\. //\w indicates all numbers and letters are underlined //\. Indicates a match., followed by a selectionCopy the code

3. Delete unnecessary Spaces

var pattern = /\s/g;
var str = '111, 222, 333';
var result = str.replace(pattern,' '); // match Spaces with no Spaces alert(result);Copy the code

4. Delete the beginning and end Spaces

var pattern = /^\s+/; // Mandatory first var STR ='goo gle '
var result = str.replace(pattern, ' '); pattern = /\s+$/; Result = result.replace(pattern,' ');
alert('|' + result + '|');Copy the code

Reference documentation