Regular expressions are really hard to read, and you can get confused after reading all the characters

In fact, we just need a few “examples” and you can get started.

Like this: (And finally, how to use regular expressions)


1. Basic syntax of regular expressions


The “^” indicates the beginning of a string

“$” indicates the end of a string

“\” marks the next character as a special character, or a literal character, or a backreference, or an octal escape

“^ ABC “: matches all strings starting with” ABC “(e.g.” ABC “, “abccba”)

“ABC $” : matches all strings ending in” ABC “(e.g.” ggGABc “, “reddCBA”)

“^ ABC $” : matches strings that start and end with “ABC” (for example, “ABC”)

“ABC” : matches any string containing “ABC” without any characters (for example, “aaaabCCC”, “abc123”)

“N” : matches n” \n” : matches the newline character “\/” here \ he/is written together to match the “/” character



The “*” matches the preceding subexpression zero or more times

The “+” matches the preceding subexpression one or more times

“? “Matches the preceding subexpression zero or once

“Ac *” : matches one of the strings of an A followed by zero or more Cs (for example,” ACCC “, “abbb”)

“Ac +” : matches one of the strings of an A followed by at least one or more c (for example: “ac”,” ACCcccccc “)

“ac?” : Matches a string in which an A is followed by zero or a C (for example, “a”, “ac”)

“a? C +$” : Matches the string with zero or one A followed by one or more CS (for example: “ac”, “ACCCCcc”, “c “, “CCCCCCC”)



“{n}” n is a non-negative integer matched n times

“{n,}” n is a non-negative integer that matches at least n times

“{n, m}” n, m is a non-negative integer that matches at least n times and at most m times

“Ab {3}” : indicates a string with an A followed by two Bs (e.g. “abb”, “abbbbb”)

“Ab {3,}” : indicates that a string has an A followed by at least two Bs (for example, “abb”, “abbb”)

“Ab {3,6}” : indicates a string with an A followed by three to six Bs (e.g., “abbb”, “abbbb”, “abbbb”)



“|” said “or”

“.” stands for any character

“A | b,” said a string in a or b (for example: “a”, “b”, “ab”, “ABC”)

“A.” : indicates a string with an A followed by an arbitrary character (for example,” A1 “, “A456”, “AVV”)



The “^” is used in square brackets to indicate unwanted characters

“[ABC],” said character set, said one string with an “a” or “b” or “c” is equivalent to | b | c [z]

“[^ ABC]” : indicates that ABC should not appear in a string, that is, matches any character that does not contain a set of changes

“[a-z]” : indicates that all letters between a and Z exist in a string

“[0-9]” : indicates that all digits between 0 and 9 exist in a string

“[^a-z]” : indicates that no letter from A to Z should appear in a string

“^[a-za-z]” : indicates that a string starts with a letter

“[0-9]%” : indicates a number preceded by a percent sign;



“\d” matches a numeric character, equivalent to [0-9]

“\D” matches a non-numeric character, equivalent to [^0-9]

“\f” matches a feed character, equivalent to \x0c and \cL

“\n” matches a newline character. Equivalent to \x0a and \cJ

“\r” matches a carriage return. Equivalent to \x0d and \cM

“\s” matches any whitespace character, including Spaces, tabs, page feeds, and so on. Equivalent to [\f\n\r\t\v]

“\S” matches any non-whitespace character. Equivalent to [^ \f\n\r\t\v]

“\t” matches a TAB character. Equivalent to \x09 and \cI

“\v” matches a vertical TAB character. Equivalent to \x0b and \cK

“\w” matches any word character including underscores. Is equivalent to “[A Za – z0-9 _]”

“\W” matches any non-word character. Is equivalent to “[^ A – Za – z0-9 _]”


2. Regular expression practice


/ ^ [0-9] {1, 20} $/

Indicates that the string consists entirely of digits, that is, matches whether the current string consists entirely of digits

[0-9] indicates that characters range from 0 to 9

{1,20} indicates the range of the number of occurrences of a string that is 1 to 20 characters in length


/ ^ {1} [a zA – Z] ([a – zA – Z0-9. _]) {5, 15} $/

It can be used to authenticate login names. It must start with letters and be at least 6 and up to 16 characters in length

^[a-za-z]{1} indicates that the first initial is a letter

([A-za-z0-9._]){5,15} This is a string consisting of at least five alphanumeric and specified special characters, starting with the second letter


/^[1][3|4|5|8][0-9]\d{8}$/

It can be used to verify mobile phone numbers. It starts with 1 and is 11 digits long

^[1] The first number is 1

[8] 3 4 5 | | | second number for 3 or 4 or 5 or 8

[0-9]\d{8} Matches a number in the range of 0-9, 8 times, so there must be at least 8 digits. That adds up to 11


/ ^ (\ w) 6, 20} {$/

Verify password

\w matching any non-word character is equivalent to “[^ A-za-z0-9_]”

(\w){6,20} matches any non-word character, at least 6 and up to 20


3. Properties and methods of the RegExp object


Now, how do we use it

Var re = new RegExp(var re = new RegExp("\ [0-9] *"); var re = /[0-9]*/; // Note: When using the constructor (new RegExp) to create a regular object, you need the normal character escape rules (preceded by a backslash \)Copy the code



Test () method: regular expression method.

The test() method is used to test whether a string matches a pattern, returning true if the string contains matching text and false otherwise.

var re = /[0-9]*/;

re.test("abc") / / returnfalse

re.test("1234") / / returntrueCopy the code



Exec () method: A regular expression method.

The exec() method is used to retrieve a match for a regular expression in a string.

This function returns an array containing the matching results. If no match is found, the return value is null.

var re = /[0-9]*/;

re.exec("abc"// Return null re.exec("1234") // return 1234. Of course, it can be complicated in practice, which requires clear nestingCopy the code


Even alone, I’m still here waiting for you


Finally, several query addresses are recommended

It’s also good to know what regular characters mean: here are two links:

Regular expression quick list at http://jquery.cuishifeng.cn/regexp.html

http://www.runoob.com/jsref/jsref-obj-regexp.html novice tutorial quick regular expressions

Regular expressions online authentication website at http://tool.oschina.net/regex/