Regular expression

1. Regular expression basics

Creating regular expressions

var reg = new RegExp('Regular content', modifier)// Constructor created
var reg = / Regular content / // Create a literal
Copy the code

Regular modifiers I,g,m;

I is case insensitive

G represents a global match

M multi-line match

Methods of regeting objects:

Test () checks if the string in the argument matches the content of the regular expression and returns a Boolean value.

var reg = /a/i;
var bool = reg.test('bkAdc');
console.log(bool)
Copy the code

Exec () returns an array representing the contents of the regular string matches in the exec() argument, not globally;

var reg = /a/i;
var arr = reg.exec('bkAdc');
console.log(arr)
Copy the code

String methods:

Search () can only find the first one, not a global match, and returns the index that found the content.

var str = 'sAkjfvsa';
var index = str.search(/a/i);
console.log(index)
Copy the code

Match () lists matched elements in an array, if no global match is found, as exec does.

var str = 'sAkjfvsa';
var arr = str.match(/a/ig);
console.log(arr)
Copy the code

Replace () replaces matched elements found. When global matching is used, all elements can be matched and replaced

var str = 'sAkjfvsa';
str = str.replace(/a/ig.'z');
console.log(str)
Copy the code

Split () splits the matched elements and returns an array.

var str = 'sAkjfvsa';
var arr = str.split(/a/ig.'z');
console.log(arr)
Copy the code

2. Metacharacters

Matches any character and is a wildcard character

\. Convert the wildcard character to a character.

var str = 'catcbtctbcabtc/t';
console.log(str.match(/c.t/g))
Copy the code
[] indicates matching any of them

[a-z] indicates the match between A and Z

In [], is an escape character, not a wildcard character.

But all parentheses in [] must be escaped.

Both \ in a character or regular expression [] are the same \

var str = 'catcbtctbcabtc/t';
console.log(str.match(/c[abcdefg]t/g));
console.log(str.match(/c[a-g]t/g))
console.log('aa\\a\a'.match(/[\\]/g));
Copy the code

[aaazzzzdddd] meaningless

[a-z] error in Unicode encoding where a is larger than Z

[a-z] error capital Z to A there are other strings in between

The first character in [] is ^, indicating that the following characters are reversed

console.log('abcdef'.match(/[^d]/g))
Copy the code

[A-zA-Z0-9] indicates all numbers and letters;

\w indicates all alphanumeric underscores

\W takes the opposite of \W

\d stands for all numbers

\D means the inverse of \D

\s stands for all Spaces

\S means the inverse of \S

/[\u4e00-\u9fa5]/ Matches Chinese characters

3. Number of repetitions

{n} indicates the repeat n times

console.log('aaaaaaaaa'.match(/a{6}/g))
Copy the code

{n,m} indicates at least n repetitions and at most m repetitions. The largest character is matched first

console.log('aaaaaaaaaaaaa'.match(/ a/g {2, 4}))
Copy the code

{n,} means at least n times

console.log('aaaaaaaa'.match(/a{1,}/g))
Copy the code

{0,} indicates that there can be no or several entries, and blank is printed

console.log('aaaaaaaa'.match(/a{0,}/g))
Copy the code

/a*/g is the same as /a{0,}/g

/a+/g is the same as /a{1,}/g

/a? /g is the same as /a{0,1}/g

Greedy versus non-greedy

Greed mode

It matches the largest characters first and considers the smallest characters later.

console.log('aaaaaaaaaaaaa'.match(/ a/g {2, 4}))
Copy the code

Non-greedy mode, which matches fewer characters first.

You can match more than one character by adding? Characters.

 var str = "

Uber's prototype driverless car is equipped with multiple cameras , < EM > lidar and sensors that can see anything within 100 meters in any direction



The second line reads, Hey hey, Second line

"
; str = str.match(/ <. *? >/g); Copy the code
Same repeat /1
var str="aaabbffddeeaaggfddssaaggeer".split("").sort().join("").match(/(\w)\1*/g)
Copy the code

/(\w)\1 refers to the number of times the matching character is repeated

4. Selection and start and end

To complete a whole, you need to operate with the start and end characters

^ Start character, indicating the start of the match from this

The $terminator must end with a character

console.log('bbaacc'.match(/^a+/g));// The requirement must be one or more A's
console.log('bbaacc'.match(/c+$/g))// The requirement must be one or more C endings
Copy the code

5, groups,

The content enclosed in ()

When using match, there is a difference between using groups with g and without G.

The element that can list each group at the index 1 of the array without adding g; Add g and you can’t find the group

If groups are not used in replace, the first argument in the following function is the regular content, and the second is the subscript of the character.

If you use groups in replace, the arguments in the following function are the content that matches the regex, and the content of each group.

var str="10[a]3[bc]".replace(/(\d+)\[([a-zA-Z]+)\]/g.function(item1,item2,item3){
           return item3.repeat(item2);
        });
Copy the code

6, assertions

Forward-looking assertions (preemptive assertions) follow

console.log('abac'.match(/a(? =b)/g))
Copy the code

Find the condition that the content immediately following is a character

console.log('abac'.match(/a(? ! c)/g))
Copy the code

Finding what follows is not a condition for a character

A forward-looking assertion (a trailing assertion) follows it

console.log('abcb'.match(/ (? <=a)b/g))
Copy the code

Must be preceded by some condition followed by the character

console.log('abcb'.match(/ (? 
      ))
Copy the code

The character that must not be preceded by a condition.