Acquaintance regular expression two first met two regular expressions

A predefined pattern 2861077989

Predefined pattern is a shorthand way of referring to some of the common mode, briefly described below:

\ D: matches any of the numbers 0-9, corresponding to [0-9]

\ D: matches all characters other than 0-9, equivalent to [^ 0-9]

\ W: match any of the letters, numbers and underscores, corresponding to [A-Za-z0-9]

\ W: In addition to all letters, numbers and underscores

\ S: matches a space (including tabs, spaces, line breaks, etc.)

\ S: matching non-space characters, corresponding to [^ \ t \ r \ n \ v \ f]

\ B: boundary matching words

\ B: non-matching word boundary, that is inside the word.

In general, the regular expression newline (\ n) will stop matching, then use the / s character class, you can include line breaks.

You can also use non-capturing group

Second, duplicate class

Exact number of pattern matching, using braces ({}) FIG. n represents exactly repeated n times, {n,} represents at least n times, {n, m} indicates repeated not less than n times, not more than m times.

/lo{2}k/.test('look') //true

/lo{2,5}k/.test(looook) //true

In the above code, the first mode designating two consecutive o, o the second mode designation consecutive 2 to 5 times

Third, the character quantifiers

Quantifier is used to set the number of times a particular pattern appears, as described below:

? : 0 indicates a pattern appears or 1, is equivalent to {0,1}

*: Indicates a pattern appear zero or more times, equal to {0}

+: Indicates a pattern occurs one or more times, equivalent to {1}

Three: greedy

var s = 'aaa';

s.match(/a+/)     //["aaa"]

Before the second part describes three quantifier characters are the maximum possible match by default, that match up to the next character does not meet the matching rules, which are called greedy.

To the non-greedy greedy mode to mode, you can add a question mark character after the quantifier

var s = 'aaa';

s.match(/a+?/)         //["a"]

In addition to Star Plus, there are non-greedy pattern of non-greedy mode

* ?: indicates that a pattern appears zero or more times, non-greedy matching pattern.

+? : Indicates that a pattern appears one or more times

Fourth, the modified character

Modified character (Modifier) ​​indicates additional mode of rule, the tail on the most regular pattern matching. Modifiers can be used singly, it may also be used together. E.g:

// single modifier

was regex = / test / in;

// multiple modifiers

was regex = / test / g;

A predefined pattern 2861077989

Predefined pattern is a shorthand way of referring to some of the common mode, briefly described below:

\ D: matches any of the numbers 0-9, corresponding to [0-9]

\ D: matches all characters other than 0-9, equivalent to [^ 0-9]

\ W: match any of the letters, numbers and underscores, corresponding to [A-Za-z0-9]

\ W: In addition to all letters, numbers and underscores

\ S: matches a space (including tabs, spaces, line breaks, etc.)

\ S: matching non-space characters, corresponding to [^ \ t \ r \ n \ v \ f]

\ B: boundary matching words

\ B: non-matching word boundary, that is inside the word.

In general, the regular expression newline (\ n) will stop matching, then use the / s character class, you can include line breaks.

You can also use non-capturing group

Second, duplicate class

Exact number of pattern matching, using braces ({}) FIG. n represents exactly repeated n times, {n,} represents at least n times, {n, m} indicates repeated not less than n times, not more than m times.

/lo{2}k/.test('look') //true

/lo{2,5}k/.test(looook) //true

In the above code, the first mode designating two consecutive o, o the second mode designation consecutive 2 to 5 times

Third, the character quantifiers

Quantifier is used to set the number of times a particular pattern appears, as described below:

? : 0 indicates a pattern appears or 1, is equivalent to {0,1}

*: Indicates a pattern appear zero or more times, equal to {0}

+: Indicates a pattern occurs one or more times, equivalent to {1}

Three: greedy

var s = 'aaa';

s.match(/a+/)     //["aaa"]

Before the second part describes three quantifier characters are the maximum possible match by default, that match up to the next character does not meet the matching rules, which are called greedy.

To the non-greedy greedy mode to mode, you can add a question mark character after the quantifier

var s = 'aaa';

s.match(/a+?/)         //["a"]

除了非贪婪模式的加号,还有非贪婪模式的星号

*?:表示某个模式出现0次或多次,匹配时采用非贪婪模式。

+?:表示某个模式出现1次或多次

四、修饰字符

修饰字符(Modifier)表示模式的附加规则,放在正则匹配模式的最尾部。修饰符可以单个使用,也可以多个一起使用。例如:

//单个修饰符

var regex=/test/i;

//多个修饰符

var regex=/test/ig;

Guess you like

Origin www.cnblogs.com/whgaw156/p/12057067.html