Regular expression: Syntax:/expression/. A slash indicates the start position.
Creation method:
1. var regObj = new RegExp ("pattern", [, "flags"]);
Pattern: A required regular expression string;
Flags: optional, and a combination of some flags.
2. var regObj =/pattern/[flags]; this method cannot enclose pattern and flags in quotation marks.
Regular Expression:
Exec: searches for matching characters in the regular expression, returns the found value, and determines its position.
Eg:
Var str = "abcdefg ";
Var reg =/cd /;
Var bol = reg.exe c (str); // If a cd is returned successfully, if a cd fails, null is returned;
Test: retrieves the value specified in the string, and returns true or false.
Eg:
Var bol = reg. test (str); // success returns true, failure returns false;
Method of string object:
Match: Find a match with one or more regular expressions;
Search: searches for values that match regular expressions;
Replace: replace the string that matches the regular expression;
Split: separates strings into string numbers. // The result is an array;
RegExp object attributes:
Global: indicates whether the RegExp object has a flag. // global query. If this attribute is not available, the operation ends after the first match;
IgnorCase: indicates whether the RegExp object has a flag I; // case insensitive;
Multiline: whether the RegExp object has a flag m; // multi-row query;
Common symbols of Regular Expressions:
/.../: Indicates the start and end of a mode;
^: Match the start of a string;
$: End of matching string;
S: any blank character;
S: any non-blank characters;
D: match a number character, level [^ 0-9];
D: any character except a number is equivalent to [^ 0-9].
W: match a number, underline or letter character, equivalent to [A-Za-z0-9];
W: any non-single character, equivalent to [^ a-zA-Z0-9];
.: Any character except line breaks;
{N}: match the first item n times;
{N ,}: match the first item n times or multiple times;
{N, m}: match the first item at least n times, but cannot exceed m times;
*: Match the first item 0 or multiple times. It is equivalent to {0 ,};
+: Match the previous item once or multiple times, equivalent to {1 ,};
? : Match the first item 0 or 1 times, equivalent to {0, 1 };
|: Indicates or. eg: [0-9] | [a-z];
(): Used for grouping;
Eg:
All letters:
Var reg =/^ [a-zA-Z] + $/; at least one letter;
Four letters:
Var reg =/^ [a-zA-Z] {4} $/; Add '^' to avoid matching "3 dedf;
11-digit QQ number. The first digit cannot be 0:
/^ [1-9] {1} d {10} $/
18-digit ID card number:
/^ D {17} (d {1} | [a-zA-Z} {1}) $/
Email:
/^ W {2,} @ w {2,} (. [a-zA-Z] {2, 4}) {1, 2} $/; so that the xx@yy.com.cn can be matched;