Regular Expressions are often used in Javascript, such as verifying whether input formats such as email addresses and birthdays are valid. The following describes the regular expression usage in JS:
I. metacharacters
. Match any character, except for line breaks
\ D Number
\ W digits or letters
\ S space, including space, tab, line break, etc.
^ The string starts with the pattern, that is, the matching pattern must start with the string
$ The string ends in the pattern, that is, the matched pattern must be at the end of the string
Ii. Qualifier
* Mode appears 0 or multiple times
+ Mode appears once or multiple times
? Mode appears 0 or 1 time
The {n} mode must contain N
A | B. Select one for both.
[ABC] character combination, one of which can be used
() Submode
Iii. Regular Expressions
Regular Expressions start and end with/, in the form of/expression/. expression is an expression consisting of the preceding characters (or metacharacters) and delimiters.
VaR RegEx =/^/d {5} $/
If (! RegEx. Test (inputfield. Value ))
// The inputfield value is invalid
Use the test method to determine whether the mode matches
The following is a common regular expression:
1. Set the date format to mm/DD/YYYY or mm/DD/yy.
The corresponding regular expression is:/^ \ D {2} \/\ D {2} \/(\ D {2} | \ D {4}) $/
Note: The/In the date must be escaped \/
2. Email
The general email format is: a string of letters or numbers @ a string of letters or numbers. A string of letters or numbers (length is 2 or 3), such as test111@163.com, 112117@qq.com
The corresponding regular expression is/^ \ W + @ \ W + \. \ W {2, 3} $/
Also consider the need to yo other mail formats, such as: aa-bb@youtube.com.nz, love_you@rock.ca
More general Regular Expressions:/^ [\ W \. -_ \ +] + @ [\ W-] + (\. \ W {2, 4}) + $/(from JavaScript head first)