Regular expression: a string matches an expression and checks whether the string contains an expression based on the corresponding expression rules
In JavaScript, regular expressions are both objects with corresponding attributes (g, I, etc.) and methods (test(),search(), etc.)
Regular expression creation:
Let reg = new RegExp(' ABC ') -- constructorCopy the code
Regular expression method:
Test -- Checks for string matches, returning true/false
/abc/.test('Who are you') //false
/are/.test('Who are you') //true
Copy the code
Search - Checks if a string is matched, returns index if matched, and -1 if not
('Who are you').search(/abc/) //-1
('Who are you').search(/are/) //4
Copy the code
Match - Finds the matched target string, returns an array, returns null for no match
('Who are you').match(/abc/) //null
('Who are you').search(/are/) //[ 'are', index: 4, input: 'Who are you', groups: undefined ]
Copy the code
Exec -- Finds a matching target string, returns an array, returns null if not matched
/abc/.exec('who are you') //null
/are/.exec('who are you') //[ 'are', index: 4, input: 'Who are you', groups: undefined ]
Copy the code
Replace - Searches for and replaces the matched target string, returns the replaced string, can be used to add, delete, replace functions
('Who are you').replace(/abc/) //Who are you
('Who are you').replace(/are/,'abc') //Who abc you
('Who are you').replace(/are/,'') //Who you
Copy the code
Split -- Splits the original string by the destination string and stores it into an array
('Who are you').split(/are/) //[ 'Who ', ' you' ]
Copy the code
Properties of regular expressions:
G global Searches for all matches
('Who are you.What are you doing').match(/are/g) //[ 'are', 'are' ]
Copy the code
I ignore Case-insensitive match is size insensitive
('What are you.what are you doing').match(/what/i) //[ 'What', 'what' ]
Copy the code
M multi Line - Multi line matching makes the boundary characters ^ and $match the beginning and end of each line, multiple lines
("What are you\nWhat are you doing").match(/What/gm) //[ 'What', 'What' ]
Copy the code
S metacharacter. Matches any character except newline /n. S metacharacter. Add newline character /n
("What are you\nWhat are you doing").match(/you./) //[ 'you ',index: 22,input: 'What are you\nWhat are you doing',groups: undefined ]
("What are you\nWhat are you doing").match(/you./s) //[ 'you\n',index: 9,input: 'What are you\nWhat are you doing',groups: undefined ]
Copy the code
Metacharacters for regular expressions:
A character assigned a special meaning
\ -- Escape character, used to escape special characters to ordinary characters, such as: /\$/ -- represents the $character, not the metacharacter $^ -- matches the start of the string, such as: /^a/ -- matches whether the start of the string is a $-- Matches the end of the string, such as: /a$/ -- Does the matching string end with a * -- match expression 0 or more, e.g. /ab*/ -- + -- match expression 1 or more, e.g. /a+/ -- match all a characters in the string? Matches the metacharacter 0 or 1 times, e.g. /a? A / -- Matches any xa character in the string,x is any character (x) -- (expression), matches the expression in parentheses, and records the expression,\ 1,\2,\3... $1,$2,$3... Represents the expression position in the original string (? :x) -- Non-capture, X \y -- matches x or y {n} -- matches the previous expression {n,} -- matches the previous expression {n,} -- matches the previous expression [xyz] between n and m times -- matches any character in [], can be used -- specified range, [a-z] -- 26 letters from a to Z [^xyz] -- Matches any character in [] except [\b] -- matches backspace \b -- matches characters on boundaries, boundaries -- start of string next to Spaces, End position \B -- Matches non-boundary character \D -- Matches number \D -- Matches non-number \ F -- matches page feed character \ N -- Matches newline character \ R -- Matches carriage return character \S - matches whitespace character \S - matches non-whitespace characterCopy the code
Regular expression examples:
// Check whether the string belongs to a phone number. // Mobile number is an 11-digit string starting with 1, followed by any digit from [3-9]. Let phone = '17883651129' let reg = /^1[3-9][0-9]{9}/ // The metacharacter ^1 checks whether the start value is 1, the metacharacter [3-9] checks whether the second digit is in the range from 3 to 9, and the following nine digits of [0-9]{9} check whether reg.test(phone) //trueCopy the code
// Make sure that your email address is correct. Lowercase letters | | digital | | underscore + (any character) +. + @ + any character +. Com let eMail = '318739742 @qq.com' let reg = / ^ [a zA - Z0 - _ - 9] \ w + @ \ w {2}. Com / reg.test(eMail) //trueCopy the code
Common regular expressions:
Address: [a zA - z] + : / / [^ \ s] *
Email: \ w + (\ w + / - +.]) * @ \ w + ([-] \ w +) * \ \ w + ([-] \ w +) *
Password: (? = ^. ({8} $)? =.*\d)(? =.*\W+)(? =.*[A-Z])(? =.*[a-z])(? ! .*\n).*$
Id card: \d{15}(\d\d[0-9xx])?