SoFunction
Updated on 2025-03-02

Regular expression daily collection and organization (simple and practical)

Regular expressions, also known as regular representations and regular representations. (English: Regular Expression, often abbreviated as regex, regexp or RE in code), a concept of computer science. Regular expressions use a single string to describe and match a series of syntactic rules. In many text editors, regular expressions are usually used to retrieve and replace text that conforms to a certain pattern.

The function of regular expressions is very powerful. I won’t say much nonsense, I will share the content organized with you directly.

1: Basic matching characters:

\d Match the number eg:'5\d0'------>'580'

\w Match letters or numbers eg:'\d\w\w'--------->'8zh'

. Match any character except line breaks eg:'zh.'---------->'zh&'

\s Whitespace (tab) or space

Two: Match character length:

* Indicates any character (including 0)

+ means at least one character

? Represents 0 or 1 character

{n} means n characters

{n,m} means n-m characters

********************************************************

Exercise questions:\d{3}\s+\d{3,8};

The answer is here 2333: The matching is made up of 3 numbers immediately followed by at least one space or a whitespace immediately followed by 3 to 8 numbers;

********************************************************

<<<<<< Add to add: Special characters like '-' need to be translated to match~ like this '\-' >>>>>>

3: But if this is the only match, it is a bit simpler. In the face of complex matches, we still need more accurate matching methods:

[0-9a-zA-Z\_] can match a number, letter or underscore, test: '0', 'a', 'Z', '_' can all match, so the superficial expression method is a composition method that can be or can be matched;

[0-9a-zA-Z\_]+ can match a string composed of at least one number or letter or underscore;

[a-zA-Z\_][0-9a-zA-Z\_]* can be matched starting with a letter or underscore, followed by any number or letter or underscore;

[a-zA-Z\_][0-9a-zA-Z\_]{0,19} The limit length is between 1 and 20, why is it 1 to 20, the string immediately following the beginning is between 0 and 19, so the maximum length is between 20;

A|B can match A or B. [J|j]aina can match jaina or Jaina;

^ means that it must start with a certain character, eg: ^\d means that it must start with a number

$ means that it must end with a certain character, eg: \d$ means that it must end with a number. Note that the expression of the two is placed in front of the character, and $ means that it is placed in front of the character.

Four: Verification method in js:

var reg='\d{3}\s+\d{3,8}';
var tel='010 123456';
((tel));//true