Knowledge to prepare

  1. We need to know the basics of regular expressions
  • Start metacharacter/ ^
  • Include metacharacters[]
  • $/End metacharacter

Mailbox verification analysis

Demand analysis

  • Want to[a-z],[A-Z],[0-9], as well as_and-At the beginning
  • You can fill in the middle as you like
  • The string must be matched with @ and immediately followed by[a-z],[A-Z],[0-9], as well as_and-Character and match at least twice
  • End with. Plus two or three letters

The source code

Export const isEmail = (e) = > {return / ^ ([a zA - Z0 - _ - 9]) + @ ([a zA - Z0 - _ - 9] {2}) + (. [a zA - Z] {2, 3}) $/. The test (e)}Copy the code

Code segment parsing

  1. /^([a-zA-Z0-9_-])This code block indicates that the initial letter can accept lowercase letters, uppercase letters, digits, and two characters – and _
  2. @([a-zA-Z0-9_-]{2,})The string must follow the @ signAt least two, lowercase letters, uppercase letters, digits, and – and _, for example@ qq, @ 163
  3. (. [a zA - Z] {2, 3}) $/Finally, the string needs to match. Add two or three charactersThe ending, for example.com,. Cn

Mobile phone number verification analysis

Demand analysis

  • Start with the existing three vendor phone number resources
  • The value is in the format of 11 digits

The source code

export const isMobile = (e) => {
  return /^(13|14|15|17|18|19)[0-9]{9}$/.test(e)
}
Copy the code

Code segment analysis

  1. / ^ (13 14 15 17 18 | | | | | 19)Beginning to13, 14, 15, 17, 18, 19Start of existing phone number resources
  2. [0-9] {9} $/Since we already have a two-digit beginning, we can follow it with nine digits

The end of the

The second article, two very simple regular expressions, can only meet some relatively simple requirements, to a small white Angle analysis and explanation, will continue to supplement some higher order regular expressions, thank you