Common JavaScript validation regular expression summary

  • 2020-03-30 00:00:15
  • OfStack

The following are some of the most common regular expressions I've collected, as they are often used for form validation. Send out specially, let each friend use jointly. Ha ha.

Regular expression matching Chinese characters: [u4e00-u9fa5]
Comments: matching Chinese is a real headache, with this expression it is easy to do

Match double byte characters (including Chinese characters) : [^x00-xff]
Note: can be used to calculate the length of a string (2 for a double-byte character, 1 for an ASCII character)

Matches the regular expression for blank lines: ns*r
Comment: can be used to delete blank lines

Regular expressions that match HTML tags: < * (S)? [^ >] * > . *? | < . *? / >
Note: the version circulating on the Internet is so bad that this one only matches parts, and it still doesn't do much for complex nested tags

A regular expression that matches the first and last blank characters: ^s*|s*$
Comment: a useful expression that can be used to remove whitespace characters at the beginning and end of a line (including Spaces, tabs, page breaks, and so on)


Matches the Email address of the regular expression: w + (+ / - +. W) * @ w + +) ([-] w *. W + +) ([-] w *
Note: forms are useful for validation

Regular expression matching URL: [a-za-z]+://[^s]*
Note: the version circulating on the Internet has very limited features. This one basically meets the requirements

Is the matching account valid (letter beginning, 5-16 bytes allowed, alphanumeric underscore allowed) : ^[a-za-z][a-za-z0-9_]{4,15}$
Note: forms are useful for validation

Match domestic phone number: d{3}-d{8}|d{4}-d{7}
Comments: match the form such as 0511-4405222 or 021-87888822

Tencent QQ number: [1-9] [0-9] {4,}
Note: tencent QQ number starts from 10000

Match Chinese postal code: [1-9] d {5} (? ! D)
Comment: China postal code is 6 digits

Matching id card: d{15}|d{18}
Commentary: Chinese id CARDS are 15 or 18

Match IP address: d+. D +. D +. D +
Comment: useful when extracting IP addresses

Match specific Numbers:
^[1-9]d*$// matches a positive integer
^-[1-9]d*$// matches a negative integer
^ -? [1-9]d*$// matches the integer
^[1-9]d*|0$// matches a non-negative integer (positive integer + 0)
^-[1-9]d*|0$// matches non-positive integer (negative integer + 0)
^[1-9]d*. D *|0. D *[1-9]d*$// matches a positive floating point number
^-([1-9]d*. D *|0. D *[1-9]d*)$// matches a negative floating point number
^ -? (1-9] [d * d * | 0. D * (1-9] d * | 0? .0+|0)$// matches floating point number
^ (1-9] d *. * d | 0. D * (1-9] d * | 0? .0+|0$// matches non-negative floating point (+ 0)
^ (- (1-9] [d *. | 0. D * * d * (1-9] d)) | 0? .0+|0$// matches non-positive floating point (negative floating point + 0)
Note: useful when dealing with a large amount of data, pay attention to correction when specific application

Match a specific string:
^[a-za-z]+$// matches A string of 26 English letters
^[a-z]+$// matches A string of 26 uppercase English letters
^[a-z]+$// matches a string of 26 lowercase English letters
^[a-za-z0-9]+$// matches A string of Numbers and 26 English letters
^w+$// matches a string of Numbers, 26 English letters, or underscores

When using RegularExpressionValidator validation controls the functions and expressions of verification is introduced as follows:

Only Numbers can be entered: "^[0-9]*$"
Only n bits: "^d{n}$"
Can only enter at least n digits: "^d{n,}$"
Only m-n digits can be entered: "^d{m,n}$"
Can only enter zero and non-zero starting Numbers: "^(0|[1-9][0-9]*)$"
Only positive real Numbers with two decimal places can be entered: "^[0-9]+(.[0-9]{2})? $"
Can only enter positive real Numbers with 1-3 decimal places: "^[0-9]+(.[0-9]{1,3})? $"
Only positive non-zero integers can be entered: "^+? [1-9] [0-9] * $"
Only non-zero negative integers can be entered: "^-[1-9][0-9]*$"
Only characters of length 3 can be entered: "^.{3}$"
Can only enter A string of 26 English letters: "^[a-za-z]+$"
Only 26 uppercase English letters of the string: "^[a-z]+$"
You can only enter a string of 26 lowercase English letters: "^[a-z]+$"
Can only enter A string of Numbers and 26 English letters: "^[a-za-z0-9]+$"
Can only enter a string of Numbers, 26 English letters, or underscores: "^w+$"
Verify user password: "^[a-za-z]w{5,17}$"

Only characters, Numbers, and underscores can be included.
Verify if ^%&',; =? $" : "[^%&',;=?$x22]+"
Can only input Chinese characters: "^[u4e00-u9fa5],{0,}$"
Verify Email address: "^w+[-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$"
Verify InternetURL: "^ http:// ([w] +) + [w] + (/ [1 w - /? % & =] *)? $"
Verify phone number: "^((d{3,4})|d{3,4}-)? D {7, 8} $"

The correct format is: "xxx-xxxxxxx", "xxx-xxxxxxxx", "xxx-xxxxxxx",

"Xxx-xxxxxxxx", "XXXXXXX", "XXXXXXXX".
Verify id number (15 or 18 digits) : "^d{15}|d{}18$"
Verify 12 months of the year: "^(0? [1-9]|1[0-2])$"
Verify 31 days of a month: "^((0? [1-9]) | | 2 (1) ([0-9]) | | 30 31) $"

The correct format is "01" "09" and "1" "31".

Regular expression matching Chinese characters: [u4e00-u9fa5]
Match double byte characters (including Chinese characters) : [^x00-xff]
A regular expression that matches an empty line: n[s|]*r
Regular expressions that match HTML tags: /< (. *) > . * | < (. *) / > /
Regular expression matching Spaces :(^s*)|(s*$)
Matches the Email address of the regular expression: w + (+ / - +. W) * @ w + +) ([-] w *. W + +) ([-] w *
Regular expression matching URL: http:// ([w] +) + [w] + (/ [1 w - /? % & =] *)?

(1) application: calculate the length of a string (2 for a double-byte character, 1 for an ASCII character)
String. The prototype. Len = function () {return this. The replace ([^ x00 - XFF] / g, "aa"). The length; }

(2) application: there is no trim function like vbscript in javascript, so we can use this expression to achieve
String. The prototype. The trim = function ()
{
Return this. The replace (/ (^ s *) | (s * $)/g, "");
}

(3) application: using regular expressions to decompose and convert IP addresses
Function IP2V(IP) //IP address is converted to the corresponding value
{
Re =/(d+).(d+).(d+).(d+).(d+)
The (IP) if (re)
{
Return the RegExp. $1 * math.h pow (255, 3)) + RegExp. $2 * math.h pow (255, 2)) + RegExp. $3 * 255 + RegExp. $4 * 1
}
The else
{
Throw new Error(" Not a valid IP address! ") )
}
}

(4) application: javascript program to extract file names from the URL address
S = "/ / www.jb51.net/page1.htm";
S = s.r eplace (/ (. *) {0} ([^.] +). * / ig, "$2"); / / Page1. HTM

(5) application: use regular expressions to restrict the text field input in the web page form
Onkeyup = "value=" /blog/value.replace(/["^u4E00-u9FA5]/g, ") "onbeforepaste=" clipboarddata.setdata ('text', clipboarddata.getdata ('text'). Replace (/[^u4E00-u9FA5]/g, ")"

Onkeyup = "value=" /blog/value.replace(/["^uFF00-uFFFF]/g, ") "onbeforepaste=" clipboarddata.setdata ('text', clipboarddata.getdata ('text'). Replace (/[^uFF00-uFFFF]/g, ")"

Onkeyup = "value=" /blog/value.replace(/["^d]/g, ") "onbeforepaste=" clipboarddata.setdata ('text', clipboarddata.getdata ('text'). Replace (/[^d]/g, ")"

Onkeyup = "value=" /blog/value.replace(/[W]/g, ") "onbeforepaste=" clipboarddata.setdata ('text', clipboarddata.getdata ('text'). Replace (/[^d]/g,"


Related articles: