SoFunction
Updated on 2025-04-06

Regular Expression (RegExp) Detailed explanation

Regular expression, also known as regular expression, regular expression, regular expression, regular expression, regular expression, regular expression (English: Regular expression, often abbreviated as regex, regexp or RE in code).

Regular expressions use a single string to describe and match a series of strings that conform to a certain syntactic rule. In many text editors, regular expressions are usually used to retrieve and replace text that conforms to a certain pattern.

1. Character Class

1. Character class: can match any character it contains

eg: /[abc]/ matches any of the letters "a", "b", and "c"

2. Negative character class: Define the negative character class through the "^" character, which matches all characters not included in square brackets. When defining a negative character class, use a "^" symbol as the first character in the left bracket.

eg:/[^abc]/Match all characters except "a", "b", and "c".

3.\s: matches space characters, tab characters and other Unicode whitespace characters;

.  :  Any character except line breaks and other Unicode line terminators.

2. Repeat:

1. {n,m} matches the previous item at least n times, but cannot exceed m times;

2. {n,} Match the previous item n times or more times;

3. {n} matches the previous item n times;

4.? Match the previous item 0 times or 1 time;

5. * Match the previous item 0 or more times;

6. + Match the previous item once or multiple times.

PS: Non-greedy repeat--- just follow a question mark after the character to be matched. "??" "+?" "*?"

3. Selection, Grouping and Reference

1. "|" divides the selected characters

eg: /ab|cd|ef/ can match the string "ab", it can match the string "cd", it can also match the string "ef"

2. "()": The function of parentheses: a. Combine separate items into subexpressions; b. Define sub-modes in the complete pattern; c. Allow the previous sub-expression to be referenced at the back of the same regular expression.

3. "(?:" and ")" are grouped, but do not remember characters matching the shuffle

4. Specify the matching position:

1. Match the reasonable position of occurrence, the anchor of the regular expression.

2. "^" matches the beginning of the string

3. "$" matches the end of the string

4. Any regular expression can be used as an anchor. If an expression is added between the symbol "(?=" and ")", it is a pre-emptive assertion.

5. Negative assertions "(?!" and ")"

V. Modifier:

1. i is case-insensitive

2.g Global Match

3. Perform matching in multiple rows

6. RegExp method

--------- String method ------

1. search() returns the position of the first matching string;

2. Repalce() performs search and replacement operations

If the regular expression has a modifier g set, then all the strings in the source string that match the pattern will be replaced with the string specified by the second parameter; if the modifier g is not included, only the matching first substring will be replaced.

3. match() returns an array composed of matching results

---------- RegExp method -------

4、exec()

5、test()

====================================================================

1. Delete the whitespace characters in the beginning and end lines (including space characters, tab characters, and other Unicode whitespace characters)

var pattern = /(^\s*)|(\s*$)/g ;
"  abc  def  ".replace(pattern,""); //The execution result is: "abc  def"
2. Strong password: The password is 8-12 digits, and it contains uppercase letters, lowercase letters, numbers, and special characters.

var strongRegExp = /^(?=.{8,12})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$/g ;
3. Weak password: The password is 7-12 digits, and contains any two items in uppercase letters, lowercase letters, and numbers, and does not contain special characters.

var mediumRegExp = /^(?=.{7,12})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[1-9]))|((?=.*[a-z])(?=.*[1-9]))).*$/g ;