Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of the regular expression regex

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what is the use of regular expression regex for you. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

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

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

I. character class

1. Character class: it can match any character it contains

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

2. Negative character class: the negative character class is defined by the "^" character, which matches all characters not contained in square brackets. When defining a negative character class, use a "^" symbol as the first character in the left parenthesis.

Eg:/ [^ abc] / matches all characters except "a", "b", "c".

3.\ s: matches spaces, tabs, and other Unicode spaces

. Any character except newline characters and other Unicode line Terminators

2. Repeat:

1. {ncentine m} matches the previous item at least n times, but not more than m times.

2. {n,} match the previous term n or more times

3. {n} match the previous item n times

4 、? Match the previous item 0 or 1 times

5. * match the previous item 0 or more times

6. + match the previous item one or more times.

PS: non-greedy repetition-just follow the character to be matched with a question mark. "?"+"*?"

III. Selection, grouping and citation

1. "|" split the characters for selection

Eg:/ab | cd | ef/ can match the string "ab", the string "cd", or the string "ef"

2. "()": the function of parentheses: a. Combine individual items into subexpressions; b. Define the subpattern in the complete pattern; c. Allows the previous subexpression to be referenced after the same regular expression.

3. Group "(?:" and ")", but do not remember the characters that match the group

4. Specify the matching location:

1. Match the reasonable position where it occurs and the anchor of the regular expression.

2. "^" matches the beginning of the string

3. The end of the "$" matching string

4. Any regular expression can be used as an anchor. If you add an expression between the symbols "? =" and ")", it is an antecedent assertion.

5. Negative antecedent assertion "?!" And ")"

5. Modifier:

1. I is case-insensitive

2. G global matching

3. Perform matching in multiple lines

VI. RegExp method

-String method-

1. Search () returns the position of the first matching string

2. Repalce () performs retrieval and replacement operations

If the regular expression sets the modifier g, all strings in the source string that match the pattern are replaced with the string specified by the second parameter; if there is no modifier g, only the first substring that matches is replaced.

3. Match () returns an array of matching results

-RegExp method-

4. Exec ()

5. Test ()

= some common regular expressions =

1. Delete the white space characters of the first and last lines (including space characters, tabs, other Unicode space characters)

Var pattern = / (^\ s *) | (\ s $) / g

"abc def" .replace (pattern, ""); / / the result of execution is "abc def"

2. Strong passwords: passwords are 8-12 digits and contain uppercase letters, lowercase letters, numbers and special characters.

Var strongRegExp = / ^ (? =. {8 ~ (12)}) (? =. * [A murz]) (? =. * [a Muz]) (? =. * [0-9]) (? = .*\ W). * $/ g

3. Weak password: the password is 7-12 digits and contains any two items of uppercase letters, lowercase letters and numbers, and does not contain special characters

Var mediumRegExp = / ^ (? =. {7jie 12}) ((? =. * [Amurz]) (? =. * [Amurz]) | (. * [Amurz]) (? =. * [1-9])) | (? = .* [aMuz]) (? = .* [1-9])).

This is the end of this article on "what is the use of regular expression regex". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report