According to B station teacher Ono Mori regular expression course organized notes

Course links

escape

Escape symbols and escape characters

Special escape characters

\ n line

Enter \ r

\ T TAB -> TAB key 4 Spaces

String newline

Method three: You can define multi-line strings with backquotes

Regular expression

RegExp = regular expression

Early experience

Instantiation mode

Object direct quantity

If variables are to be used, they must be instantiated

Pay attention to

The modifier gim

The expression []

[]

What is each of the three consecutive characters

Range of writing

^ symbol

| or

metacharacters

Metacharacters are escape characters used by the re

\w \W

\d \D

\s \S

Matches a whitespace character

\b \B

.

Can match all characters except carriage return \r and newline \n

quantifiers

Rules of regular matching:

From left to right, the string matches more and then less. If the string matches once, it does not match back.

Greedy matching principle: can match many, never match few

n+

{1, plus infinity}

n*

{0, plus infinity}

An asterisk is a string that matches zero or more occurrences of any character in the range n,

If you match it multiple times first, after you match it multiple times, the null character counts as zero, so that’s fine.

n?

{0, 1}

n{x,y}

Do not scribble Spaces in regular expressions

Y does not write the default {1, infinity} === n+

X =0,y does not write default {0, infinity} === n*

^n

Matches any string beginning with n, this example is wrong, should be [‘ab’, ‘ab’]

n$

Matches any string ending in n,

The problem

Check whether the string starts and ends with abcd

Check whether the string starts or ends with abcd

Check whether the string starts and ends with abcd and has a number in the middle

Reg = / ^ abcd [\ d] + abcd $/ can

Matches an 11-digit cell phone number starting with 138

a(? =n)

A forward lookup matches string A immediately after the specified string n

a(? ! n)

The match is followed by string A that does not specify string n, also known as forward lookup

xxxx xxyy

subexpression

(a) in parentheses are subexpressions

Subexpressions are memorized, and subsequent backreferences are considered to match the same character

backreferences

\1 means that you backreference the first subexpression

\n means that you backreference the NTH subexpression

xxyy

(? :n)

Str.match catches the subexpression as well

Capture the packet

All three subexpressions a, b, and c are captured

No capture grouping

(? :a) disallow the capture of this subexpression

Regular object properties

Regular object method

reg.test(str)

reg.exec()

This method is a mechanical loop

features

Need to know the phenomenon

The index property in the output array is the same as the reg.lastIndex property, and can be changed by changing the reg.lastIndex value, that is, changing the matching position

But if you change lastIndex to something other than a multiple of 3, the next index will be the smallest multiple of 3 greater than lastIndex:

When reg. Exec (STR) is executed, lastIndex automatically synchronizes with index to 9

str.match(reg)

Mobile phone number check

Date of birth check

The difference between Match and Exec

str.split(reg)

String splitting

The method of the object constructed by String

str.toUpperCase()

str.toLowerCase()

str.toLocaleUpperCase()

str.toLocaleLowerCase()

For some special languages, such as Turkish, which have special letters and case, the locale is used to convert the case of these languages

The case conversion function can be implemented for English letters with or without locale

str.charAt()

str.charCodeAt()

str.fromCharCode()

str.substr()

Length after front closure

str.substring()

Before closed after opening

ValueOf and toString comparison

1. ToString String valueOf returns the original value

2, Date: toString indicates the time unique string value0f 13-bit millisecond timestamp

3, array: toString array elements used, split the string value0f array original value

Object: The type of the toString object. The string represents the original value of the object ‘[Object object]’ value0f

5. ToString function distinguishes between built-in JS functions and custom functions through [native code]

6, toString receives a parameter – radix Value0f has no parameter

Encapsulate utility class functions

The first kind of

The second,

Greedy mode and non-greedy mode

Greed mode by default, plus one? Go into non-greedy mode

Example 1

Example 2

replace

He doesn’t have the global matching capability

First argument: the string to replace

The second argument: the string to replace

xxyy -> yyxx

Method one:

Method 2:

js-plus-plus -> jsPlusPlus

Parentheses must be used for subexpression

jsPlusPlus -> js_plus_plus

aabbcc -> a
b b
c$

If you use the $sign, you should write two: $$

You could view it as 1 $$2 $3

Zero or more occurrences

1000000000000 – > 1000000000000

Tip: Replace empty Spaces with commas

Matches that are followed by 1 or more 000 and the string ends with their null, change this null to a comma

The template to replace

Double brace substitution

Always remember to write subexpressions

Template replacement (critical)

Input fields do not allow input Spaces

At least 6

Validation email

Pay attention to the point

Any symbols in the syntax must be escaped, otherwise a syntax error will be reported