This is the third day of my participation in the August More text Challenge. For details, see:August is more challenging

Overview of regular expressions

Regular expressions are used to match patterns of combinations of characters in a string. In JavaScript, regular expressions are also objects

Regular tables are often used to retrieve and replace text that conforms to a pattern (rule), such as validation forms: the user name table can only enter English letters, numbers, or underscores, and the nickname input box can enter Chinese (match). In addition, regular expressions are often used to filter out sensitive words in the content of a page (substitution), or to get a specific part of a string we want (extraction), etc

Features of regular expressions
  1. Very flexible, logical and functional
  2. Copy control of strings can be achieved quickly and in a simple way
  3. ^\w+([-+.]\w+)@\w+([-.]\w+).\w*$
  4. In practice, regular expressions are usually copied directly, but they are required to use regular expressions and modify them according to the actual situation

Creating a regular expression

There are two ways to create regular expressions in JavaScript

  1. By calling the RegExp object’s constructor

Var x = new RegExp(/ expression /);

  1. Created by literals

Var x = /;

Test the regular expression test

Test () is a regular object method that detects whether a string conforms to this rule. The object returns true or false and takes the test string as an argument

regexObj.test(str);

  1. RegexObj is a written regular expression
  2. STR the text we want to test
  3. It checks to see if the STR text conforms to the regular expression specification we wrote

Special characters in regular expressions

A regular expression can be composed of simple characters, such as/ABC /, or a combination of short and special characters, such as/ABC /. Special characters, also called metacharacters (*), are special symbols with special meanings in regular expressions. Such as ^ $+ -, etc

Boundary operator

A boundary character (positioner) in a regular expression is used to indicate the position of a character. There are mainly two characters

Boundary operator instructions
^ Represents the text at the beginning of the line to match (who starts with)
$ Represents the text at the end of the line that matches (with whom)
Var rg = / ABC /; Console. log(rg.test(' ABC ')); console.log(rg.test('abcd')); console.log(rg.test('aabc')); console.log('----------------------------'); var reg = /^abc/; console.log(rg.test('abc')); //true console.log(rg.test('abcd')); //true console.log(rg.test('aabc')); //false console.log('-------------'); Var reg = /^ ABC $/; Console. log(rg.test(' ABC ')); //true console.log(rg.test('abcd')); //false console.log(rg.test('aabc')); //false console.log(rg.test('abcabc')); //falseCopy the code

If ^ and $are together it must be an exact match

Character classes

[] indicates that you have a list of characters to choose from. You only need to match one of them

[-] Indicates the range

var rg = /[abc]/; Consloe.log (rg.test(' Andy ')); //true var rg1 = /^[abc]$/; Console. log(rg1.test('a')); // Return true if a or b or C; //true console.log(rg1.test('b')); //true console.log(rg1.test('c')); //true console.log(rg1.test('aa')); //false console.log(rg1.test('abc')); //false console.log(rg1.test('bb')); //false consloe.log('--------------'); var rg2 = /^[a-z]$/; Return true console.log(rg2.test('a')); // Return true console.log(rg2.test('a')); //true console.log(rg2.test('z')); //true console.log(rg2.test('A')); Var reg = /^[a-za-z0-9]$Copy the code

If you have a ^ in brackets, it means you’re taking the opposite. Don’t confuse it with the boundary

var reg1 = /^[^ a-z]$/; So you can’t take these

Quantifier operator

Quantifiers are used to set the number of times a pattern occurs

quantifiers instructions
* Repeat zero or more times
+ Repeat one or more times
? Repeat zero or one time
{n} Repeated n times
{n,} Repeat n or more times
{n,m} Repeat n to m times
Var reg = /^a*$/; var reg = /^a*$/; console.log('a'); //true console.log(''); //true console.log('aaa'); //{3} = 3 {3} = 3 {3} = 3 {3} = 3 {3} = 3 {16} = 3 to 16 {3} = 3 to 16Copy the code

Predefined classes

Predefined classes are shorthand for certain common patterns

Predefined classes instructions
\d Matches any number between 0 and 9, equivalent to [0-9]
\D Matches all characters except 0-9, equivalent to [^0-9]
\w [a-za-z0-9_] [a-za-z0-9_] [a-za-z0-9_]
\W All characters except letters, digits, and underscores
\s Match Spaces (including newline, TAB, space) equal to [\t\r\n\v\f]
\S Matches characters that are not Spaces

Substitutions in regular expressions

The replace() method performs the replace string operation. The replace parameter can be a string or a regular expression

stringObject.replace(refexp/substr,replacement)

  1. First argument: the string or regular expression to be replaced
  2. Second argument: the string replaced by
  3. The return value is a new replaced string

Regular expression parameter

/ expression /[switch] / Andy /g

In what pattern does the switch(also known as the modifier) match, with three values

  • G: global match
  • I: Ignores case
  • Gi: global match + ignore case