1. Create regular expressions

letThe variable name =new RegExp(/ 123 /) orletThe variable name =/ expression /
Copy the code

2, use,

Expression. Test (string to test)Copy the code

Returns true if true, false otherwise

3. Special characters in regular expressions

3.1 Boundary ^$

Start here

The $ends there

/^abc/

/^abc$/
Copy the code

3.2 or [] (only one of them)

/^[abc]$/
Copy the code

Range of 3.3 –

/^a-z$/
Copy the code

3.4 take the [^]

/ ^ ^0-9] $/Copy the code

4, quantifier *+? {}

  • Zero or many times
  • Occur 1 or more times

? Zero or one

{} is repeated many times

How many times {number,} is greater than

{number 1, number 2} is greater than or equal to number 1, and less than or equal to number 2

5. Predefined classes

\d matches between 0-9 [0-9]

\D matches all characters except 0-9 [^0-9]

\w Matches all letters, digits, and underscores [A-za-z0-9_]

\W Matches all characters except letters, digits, underscores, and [^ A-za-z0-9_]

\s Matches Spaces (newlines, tabs, Spaces, etc.) [\t\r\n\v\f]

\S Matches non-space characters [^\t\r\n\v\f]

6, replace,

String.replace(re, need to replace content)Copy the code