Regular expressions common knowledge as well as some small exercises Reg

# Regular Expressions

 

Regular expression is an expression used to find a specified [Features] expression, matching rules.

 

# Regular expressions to define the way

```

var reg = /abc/;

var reg = new RegExp('abc');

Recommended use literal way, as arrays and objects, create recommended literal way

# Use regular expressions

```

reg.test(str);

str.match(reg);

Specialties # i / g / m

 

G is the global representative of the global match

 

i ignore case on behalf of ignoreCase,

 

m represents a plurality of rows multiline match,

 

# Special characters

 

Characters that have special meaning.

\ Escape character

| Or

 

## Square brackets  

```

[Abc] to find any single character in square brackets

[^ Abc] Find any character not in the brackets

[0-9] to find a number between 0-9

[Az] find any lowercase letters

[AZ] find any uppercase letters

[Az] to find any letter

(Red | blue | green) specified search string (subexpression)

 

## metacharacters

```

Find a single character, except for line breaks and line endings.

\ W to find a word character

\ W Find a non-word character

\ D to find digital

\ D Find a non-digital

\ S Find a blank character

\ S Find a non-whitespace characters

\ B word boundary Find

\ B Find a non-word boundary

\ N Find a newline

\ F Find feed character

\ R Find carriage return

\ T Find tab

\ V Find vertical tab

\ Uxxx find Unicode characters to sixteen hexadecimal number xxxx provisions

## quantifier

 

The following are to follow greedily match:

 

n + n matches any character includes at least one

n * matches any character contains zero or more occurrences of n

n? matches any character or a 0 n of

n {X} string matching sequence of X n,

n {X, Y} string matching the sequence of Y to X n,

n n $ string ending with an

^ N matches any string beginning with n

? N = any string that matches the specified string is immediately followed by the n

?! N matches any string that is not immediately followed by the specified string n

 

Non-greedy match:

n+?    n*? ...

## supplement

```

(:? N) matching sub-expression is not this one (do not fill won grouping)

# RegExp object properties

Check whether the global RegExp object that has the g flag

Check whether ignoreCase RegExp object with the i mark

Check whether multiline RegExp object with the m mark

View source regular expression source text

lastIndex an integer flag starts the next character position matches (typically used with Exec)

 

# RegExp object methods

compile compile regular expression. (This is to change the regular expression)

Exec value specified in the search character string. The return value of the find and determine its location.

test retrieve the value specified in the string. Returns true or false.

# String method

Regular expression search retrieved value matches

Find all match results are in line with the regular matching condition

Alternatively replace string matching the regular expression

The split into an array of strings (Note: splitting the word will remain if expression subexpression)

 

```

 

 

// Regular Expression form aabb into bbaa

// var str = 'AABBCCDD';

// var reg = / (\ w) \ 1 (\ w) \ 2 / g;

// var str1 = str.replace(reg, function($, $1,$2) {

// return $2 + $2 + $1 + $1;

// })

// console.log(str1); //bbaaddcc

 

// 10 billion three RBI to become 10.000.000.000

// var str = '10000000000';

// var reg = /(?=(\B)(\d{3})+$)/g;

// str.replace(reg, '.'); //10.000.000.000

 

// string abc to become heavy aaaaaaaaaaaaaaaaaaaaaabbbbbbbbcccccccccc

// var str = 'aaaaaaaaaaaaaaaaaaaaaabbbbbbbbcccccccccc';

// var reg = / (\ w) \ 1 * (\ w) \ 2 * (\ w) \ 3 * / g;

// str.replace(reg, function($, $1, $2, $3){

// return $1 + $2 + $3;

// }); //"abc"


 

// convert the-first-name into small hump formula theFirstName

 

// var str = 'the-first-name';

// var reg = / - (\ w) / g;

// str.replace(reg, function($, $1) {

// return $1.toUpperCase();

// }) //theFirstName


 

// match the end of the digital

 

// var str = 'dkfdj1232';

// var reg = /\d$/g;

// str.match(reg); //["2"]

 

// unified number of spaces

// var str = 'fkjdf';

// var reg = /\s+/g;

// str.replace(reg, ' ');//" fkjdf "

 

Analyzing // string is not formed by a digital

 

// var str = 'fdjfd214234234';

// var reg = / (\ d * ?: [a-zA-Z]) / g;

// reg.test(str);// false


 

// delete the spaces in the string

// delete trailing spaces

// var str = 'd kdfjd dfjdkf';

// var reg = /(^\s*)|(\s*)$/g;

// str.replace(reg, '');//"d kdfjd dfjdkf"

// remove all spaces

// var reg = /(\s*)/g;

// str.replace(reg, '');//"dkdfjddfjdkf"


 

// ID number match

// var str = '12345678976543879X';

// var reg = / (^ \ d {15} $) | (^ \ d {18} $) | (^ \ d {17} (\ d | X | x) $) /; // need to globally match

// reg.test(str);

 

// e-mail address verification

was str = '[email protected]';

was reg = /^[0-9a-zA-Z_-]+@[0-9a-zA-Z_-]+(\.[0-9a-zA-Z_-]+)+$/;

reg.test(str);

 

Published 19 original articles · won praise 58 · views 50000 +

Guess you like

Origin blog.csdn.net/cyg_l02/article/details/84945345