This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!

Refer to the address: www.runoob.com/regexp/rege…

Why to write the reason is, do front-end for a long time, but the regular writing or Baidu, so here to use all write, next time there is a need, directly see their own writing.

Common expression

Mobile phone number check

The validation rule uses ^ [] \d {} $, which stands for

  1. Exclude numbers other than 1
  2. [3-9] Numbers in this range
  3. \d matches numeric characters
  4. {3,9} from the third digit, replace 9 digits
  5. $plays an end role

Assuming you did not write the last $, it is true even if it exceeds the number of digits

let phone = / ^ 1/3-9 \ d {3, 9} $/
consoleLog (phone. Test (phone number))// true
Copy the code

Mail check

  1. ^[a-Za-Z0-9] The first digit must start with a uppercase letter or digit
    • Up and second splices left and right
  2. \w the second place is more random, numbers, letters, underscores
  3. At sign splicing at sign
  4. The last is a whole, divided into the first part numbers, letters plus. And the second half of the numbers, letters
let email = /^[a-zA-Z0-9]+\w+@([a-zA-Z0-9]+\.+[a-zA-Z0-9])/
consoleLog (email.test(email number))Copy the code

The concept of regularity

Regular Expression is a text pattern that includes both ordinary characters (for example, letters between a and Z) and special characters (called “metacharacters”).

A regular expression uses a single string to describe and match a series of strings that match a syntactic rule.

grammar

A regular expression describes a pattern of string matching. It can be used to check whether a string contains a substring, replace a matching substring, or take a substring that matches a certain condition from a string.

Normal character

[ABC] matches all characters in [***]

let string = 'abc1efg'
console.log(string.replace(/[0-9]/g.'M'))
// abcMefg
Copy the code

[^ABC] matches all characters except the characters in [***]

let string = 'abc1efg'
console.log(string.replace(/[^0-9]/g.'M'))
// MMM1MMM
Copy the code

[A-z] represents an interval

Indicates a range, matching all uppercase letters, and [a-z] indicates all lowercase letters.

let string = 'ABCedf1'
console.log(string.replace(/[A-Z]|[a-z]/g.'0'))
/ / 0000001
Copy the code

.

[\s\ s] matches all

\s matches all whitespace, including newlines, \s non-whitespace, excluding newlines.

let string = `a b c d
e f 1 *`
console.log(string.replace(/[\s\S]/g.'0'))
/ / 000000000000000
Copy the code

\w matches letters, digits, and underscores

Equivalent to [A Za – z0-9 _]

let string = `a b c d _
e f 1 *`
console.log(string.replace(/[\s\S]/g.'1'))
/ / 11111111111111111
Copy the code

Nonprinting character

Non-printing characters can also be part of regular expressions.

\cx

Matches the control character specified by x. For example, \cM matches a control-m or carriage return character. The value of x must be a-Z or one of a-z. Otherwise, treat C as a ‘c’ character in its original sense.

\f

Matches a page break. This is equivalent to \x0c and \cL.

\n

Matches a newline character. This is equivalent to \x0a and \cJ.

\r

Matches a carriage return character. This is equivalent to \x0d and \cM.

\t

Matches a TAB character. This is equivalent to \x09 and \cI.

\v

Matches a vertical TAB character. This is equivalent to \x0b and \cK.

Special characters

$matches the end of the input string

let string = 'a b c a'
console.log(string.replace(/a$/g.'2'))
// a b c 2
Copy the code

() marks the beginning and end of a subexpression

Marks the start and end positions of a subexpression. Subexpressions can be retrieved for later use. To match these characters, use (and).

* Matches the previous subexpression zero or more times

let string = 'abc'
console.log(string.replace(/ab*/gim.'3'))
// 3c
Copy the code

+ matches the previous subexpression one or more times

Matches the previous subexpression one or more times. For example, ‘zo+’ matches “zo” and “zoo”, but not “z”. + is equivalent to {1,}.

Matches any single character other than the newline \n character

Matches any single character other than the newline \n character. To match a., use \.

Marks the beginning of a bracket expression.

To match [, use \ [.

? Matches the previous subexpression zero or one times

For example, “do (es)?” Matches “does” in “do”, “does” in “does”, and “do” in “doxy”. ? Equivalent to {0, 1}

\ marks the next character as either a special character, or a literal character, or a backreference, or an octal escape

For example, ‘n’ matches the character ‘n’. ‘\ n’ matches newline characters. The sequence ‘\ \ ‘matches “\ “, and ‘\ (‘ matches “(“.

^ Matches the start of the input string

Matches the starting position of the input string unless used in a square bracket expression, when used in a square bracket expression, it indicates that the set of characters in that square bracket expression is not accepted

{marks the beginning of a qualifier expression

To match {, use {.

| indicate a choice between the two

To match |, please use the |.

qualifiers

{n} n is a non-negative integer

N is a non-negative integer. Matches the specified n times. For example, ‘o{2}’ can’t match the ‘o’ in ‘Bob’, but it can match the two o’s in ‘food’.

{n,} n is a non-negative integer.

At least n matches. For example, ‘o{2,}’ cannot match the ‘o’ in ‘Bob’, but it can match all the ‘o’ in ‘foooood’. ‘o{1,}’ is equivalent to ‘o+’. ‘o{0,}’ is equivalent to ‘o*’.

{n,m} m and n are non-negative integers, where n <= m.

. At least n times and at most m times are matched. For example, “o{1,3}” will match the first three o’s in “fooooood”. ‘o{0,1}’ is equivalent to ‘o? ‘. Note that there can be no space between a comma and two numbers.

locator

\b matches a word boundary, the position between a word and a space.

let string = 'abcab ab'
console.log(string.replace(/\bab/g.'34'))
// 34cab 34
Copy the code

\B Non-word boundary matching.

let string = 'abcab ab'
console.log(string.replace(/\Bab/g.'34'))
// abc34 ab
Copy the code

The modifier

I ignore – Case insensitive

Set matching to case-insensitive and search to case-insensitive: there is no difference between A and A.

G global – Global match

Look for all matches.

M Multi line – Matches multiple lines

Make the boundary characters ^ and $match the beginning and end of each line, remember multiple lines, not the beginning and end of the entire string.

S special character dot. Contains the newline \n

The default dot. Is to match any character except the newline \n, with the s modifier,. Contains the newline \n character.

metacharacters

The elements of metacharacters are similar to most in the syntax, so write only those that are different.

\ marks the next character as a special character, or a literal character, or a backreference, or an octal escape character

For example,'n'The matching characters"n".'\n'Matches a newline character. The sequence'\ \'matching"\","\ ["Then match"(".Copy the code

Matching x | y x or y

\d matches a numeric character

Equivalent to [0-9]

\D matches a non-numeric character

This is equivalent to [^0-9].

\w matches letters, digits, and underscores. This is equivalent to ‘[a-za-z0-9_]’.

\W matches non-letters, digits, and underscores. This is equivalent to ‘[^ a-za-z0-9_]’.

\xn matches n, where n is a hexadecimal escaped value

The hexadecimal escape value must be two digits long. For example, ‘\x41′ matches’ A ‘. ‘\x041’ is equivalent to ‘\x04’ & “1”. ASCII encoding can be used in regular expressions.

\num matches num, where num is a positive integer.

A reference to the match obtained. For example, ‘(.). \1’ matches two consecutive characters of the same character.

\nm identifies an octal escape value or a backreference

If \nm is preceded by at least nm obtain subexpressions, then nm is a backreference. If \nm is preceded by at least N acquires, n is a backreference followed by the text m. If none of the preceding conditions is met, \nm will match the octal escape value nm if both n and m are octal digits (0-7).

\ NML Matches the octal escape value NML if n is an octal number (0-3) and m and L are both octal numbers (0-7).

\ UN matches n, where n is a Unicode character represented by four hexadecimal digits.

For example, \u00A9 matches the copyright symbol (?)

Operator precedence

  1. Regular expressions are evaluated from left to right and in priority order
  2. Operations of the same priority are performed from left to right, and operations of different priorities are performed first and then lower.

\ escape character

(), (? :), (? =), [] parentheses and square brackets

*, +,? , {n}, {n,}, {n,m} qualifiers

^, $\ any metacharacters, anchor point and any character sequence (i.e., the position and order) |

| replace, “or” operations

Character is higher than replace the operator priority, make “m | food” matching “m” or “food”. If you want to match the “mood” or “food”, please use parentheses to create the expression, resulting in a “(m | f ood)”.

? =,? < =,? ! ,? <! Use of

x(? =y) look for x in front of y

let string = 'abc'
console.log(string.replace(/a(? =b)/g.'5'))
// 5bc
Copy the code

(? <=y)x Looks for x after y

let string = 'abac'
console.log(string.replace(/ (? <=b)a/g.'5'))
// ab5c
Copy the code

x(? ! Y) look for x that doesn’t follow y

let string = 'abac'
console.log(string.replace(/a(? ! b)/g.'5'))
// ab5c
Copy the code

(? <! Y)x looking for an x that doesn’t have a y in front of it.

let string = 'abac'
console.log(string.replace(/ (? 
      .'5'))
// 5bac
Copy the code

Related articles

Basic Usage of mongodb

Flex Layout Summary

conclusion

Regular expression is usually not much attention, when using a special headache, this is my feeling. So according to the document, wrote a time, some of their own experiment again, there is a wrong place, but also please a lot of guidance, welcome to thumbs up, leave a message!!