Regular expression regular Expression (REGEXP) Detailed _ Regular expression

Source: Internet
Author: User
Tags first string lowercase modifier strong password

Regular expressions, also known as formal representations, formal representations, regular expressions, regular expressions, regular representations (English: Regular Expression, often abbreviated as regex, RegExp, or re) in code.

A regular expression uses 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 often used to retrieve and replace text that conforms to a pattern.

One, character class

1, character class: You can match any of the characters he contains

eg:/[abc]/matches any one of the letters "a", "B", "C"

2, negative character class: by "^" character defines a negative character class, he matches all the characters not included in square brackets. When defining a negative character class, a "^" symbol is used as the first character in the left parenthesis.

eg:/[^abc]/matches all characters except "A", "B", "C".

3, \s: Matching is spaces, tab and other Unicode blank characters;

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

Second, repeat:

1, {n,m} match the previous one at least n times, but not more than m times;

2, {n,} matches the previous n times or more times;

3, {n} match the previous n times;

4,? Match 0 or 1 times before;

5, * Matching the previous 0 or more times;

6, + match the previous 1 or more times.

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

Iii. Select, group, and reference

1, "|" Split to select characters

eg:/ab|cd|ef/can match the string "AB", can also match the string "CD", and can also match the string "EF"

2, "()": The function of parentheses: A. Combine individual items into a subexpression; B. Define the child mode in the complete schema; c. Allow the preceding subexpression to be referenced in the back of the same regular expression.

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

Four, specify the matching location:

1, matching the occurrence of a reasonable position, the regular expression of the anchor.

2, "^" match the beginning of the string

3, "$" match the end of the string

4, any regular expression can be used as an anchor point. If you add an expression between the symbol "(? =" and "), it is a predicate assertion.

5, negative first assertion "(?!") and ")"

Five modifier characters:

1, I do not case sensitive

2, G Global matching

3, m multiple-line execution match

Vi. Methods of RegExp

-------String Method-------

1. Search () returns the position of the first string to match;

2. Repalce () perform retrieve and replace operations

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

3. Match () returns an array of matching results

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

4, EXEC ()

5. Test ()

======================= Some common regular expressions ========================

1, delete the trailing line of white space characters (including spaces, tabs, other Unicode whitespace)

var pattern =/(^\s*) | (\s*$)/g;
"ABC def". Replace (Pattern, ""); The result is: "ABC def"
2, strong password: 8-12-bit password, and uppercase, lowercase letters, numbers, special characters are included

var strongregexp =/^ (? =.{ 8,12}) (? =.*[a-z]) (? =.*[a-z]) (? =.*[0-9)) (? =.*\w). *$/g;
3, weak password: 7-12-bit password, and contains uppercase letters, lowercase letters, any number of two items, and does not contain special characters

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.