SoFunction
Updated on 2025-02-28

Summary of commonly used JavaScript verification regular expressions

Below are some of the more commonly used regular expressions I have collected, because I may use them more often during form verification. Special posting for all friends to use. hehe.

Regular expression matching Chinese characters: [u4e00-u9fa5]
Comment: Matching Chinese is really a headache, it's easy to do with this expression

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

Regular expression matching blank lines: ns*r
Comment: Can be used to delete blank lines

Regular expressions matching HTML tags: < (S*?)[^>]*>.*?|< .*? />
Comment: The version circulating online is too bad, and the above one can only match the part, and it is still powerless to use complex nested markers.

Regular expression matching the beginning and end whitespace characters: ^s*|s*$
Comment: It can be used to delete whitespace characters at the beginning and end of the line (including spaces, tabs, page breaks, etc.), a very useful expression


Regular expression matching the email address: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
Comment: It is very practical when verifying the form

Regular expression matching URL: [a-zA-z]+://[^s]*
Comment: The functions of the version circulating online are very limited, and the above can basically meet the needs

Match whether the account is legal (beginning with letters, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Comment: It is very practical when verifying the form

Match domestic phone number: d{3}-d{8}|d{4}-d{7}
Comment: Matching forms are as follows: 0511-4405222 or 021-87888822

Match Tencent QQ number: [1-9][0-9]{4,}
Comment: Tencent QQ number starts at 10,000

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

Match ID card: d{15}|d{18}
Comment: China's ID card is 15 or 18 digits

Match ip address: d+.d+.d+.d+
Comment: It is useful when extracting IP addresses

Match specific numbers:
^[1-9]d*$  ​​//Match positive integer
^-[1-9]d*$ //Match negative integers
^-?[1-9]d*$ //Match integers
^[1-9]d*|0$ //Match non-negative integers (positive integer + 0)
^-[1-9]d*|0$ //Match non-positive integers (negative integer + 0)
^[1-9]d*.d*|*[1-9]d*$ //Match positive floating point number
^-([1-9]d*.d*|*[1-9]d*)$ //Match negative floating point numbers
^-?([1-9]d*.d*|*[1-9]d*|0?.0+|0)$ //Match floating point numbers
^[1-9]d*.d*|*[1-9]d*|0?.0+|0$ //Match non-negative floating point numbers (positive floating point numbers + 0)
^(-([1-9]d*.d*|*[1-9]d*))|0?.0+|0$///Match non-positive floating point numbers (negative floating point numbers + 0)
Comment: It is useful when processing large amounts of data, please pay attention to corrections when applying it in detail.

Match a specific string:
^[A-Za-z]+$//Match a string composed of 26 English letters
^[A-Z]+$//Match a string composed of 26 English letters capitalizations
^[a-z]+$//Match a string composed of 26 English letters lowercase
^[A-Za-z0-9]+$//Match a string composed of numbers and 26 English letters
^w+$//Match a string composed of numbers, 26 English letters or underscores

The verification function and verification expressions are introduced when using RegularExpressionValidator to verify the control as follows:

Only enter numbers: "^[0-9]*$"
Only n-digit numbers can be entered: "^d{n}$"
Only at least n digits can be entered: "^d{n,}$"
Only the number of m-n bits is entered: "^d{m,n}$"
Only numbers starting with zero and non-zero are entered: "^(0|[1-9][0-9]*)$"
Only positive real numbers with two decimal places can be entered: "^[0-9]+(.[0-9]{2})?$"
Only positive real numbers with 1-3 decimal places can be entered: "^[0-9]+(.[0-9]{1,3})?$"
Only non-zero positive integers can be entered: "^+?[1-9][0-9]*$"
Only non-zero negative integers can be entered: "^-[1-9][0-9]*$"
Only characters with length 3 can be entered: "^.{3}$"
Only strings composed of 26 English letters can be entered: "^[A-Za-z]+$"
Only strings composed of 26 capital English letters can be entered: "^[A-Z]+$"
Only strings composed of 26 lowercase English letters can be entered: "^[a-z]+$"
Only strings composed of numbers and 26 English letters can be entered: "^[A-Za-z0-9]+$"
Only strings composed of numbers, 26 English letters or underscores can be entered: "^w+$"
Verify user password: "^[a-zA-Z]w{5,17}$" is the correct format: start with a letter, with a length between 6-18.

Only characters, numbers, and underscores are included.
Verify that it contains characters such as ^%&',;=?$": "[^%&',;=?$x22]+"
Only enter Chinese characters: "^[u4e00-u9fa5],{0,}$"
Verify the email address: "^w+[-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$"
Verify InternetURL: "^http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$"
Verify phone number: "^((d{3,4})|d{3,4}-)?d{7,8}$"

The correct format is: "XXXX-XXXXXXX", "XXXX-XXXXXXXXX", "XXX-XXXXXXXXX",

“XXX-XXXXXXXX”,“XXXXXXX”,“XXXXXXXX”。
Verify ID number (15 or 18 digits): "^d{15}|d{}18$"
Verify the 12 months of one year: "^(0?[1-9]|1[0-2])$" is correct in the format: "01"-"09" and "1" and "12"
31 days of verification for one month: "^((0?[1-9])|((1|2)[0-9])|30|31)$"

The correct formats are: "01", "09", and "1", "31".

Regular expression matching Chinese characters: [u4e00-u9fa5]
Match double-byte characters (including Chinese characters): [^x00-xff]
Regular expression matching blank lines: n[s| ]*r
Regular expression matching HTML tags: /< (.*)>.*|< (.*) />/
Regular expression matching the beginning and end spaces: (^s*)|(s*$)
Regular expression matching the email address: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
Regular expression matching URL: http://([w-]+.)+[w-]+(/[w- ./?%&=]*)?

(1) Application: Calculate the length of the string (one double-byte character length meter 2, ASCII character meter 1)
=function(){return ([^x00-xff]/g,”aa”).length;}

(2) Application: There is no trim function like vbscript in JavaScript, we can use this expression to implement it
= function()
{
return (/(^s*)|(s*$)/g, “”);
}

(3) Application: Decompose and convert IP addresses using regular expressions
function IP2V(ip) //Convert IP address to corresponding value
{
re=/(d+).(d+).(d+).(d+)/g // Regular expression matching IP address
if((ip))
{
return RegExp.$1*(255,3))+RegExp.$2*(255,2))+RegExp.$3*255+RegExp.$4*1
}
else
{
throw new Error(”Not a valid IP address!”)
}
}

(4) Application: a javascript program that extracts file names from URL addresses
s=”https:///”;
s=(/(.*/){0,}([^.]+).*/ig,”$2″) ; //

(5) Application: Use regular expressions to restrict the input content of the text box in the web form
Use regular expressions to restrict only Chinese: onkeyup=”value=”/blog/(/["^u4E00-u9FA5]/g,”) ” onbeforepaste=”('text',('text').replace(/[^u4E00-u9FA5]/g,”))”

Use regular expressions to restrict only full-width characters: onkeyup=”value=”/blog/(/["^uFF00-uFFFF]/g,") ” onbeforepaste=”('text',('text').replace(/[^uFF00-uFFFF]/g,"))”

Use regular expressions to limit only numeric input: onkeyup=”value=”/blog/(/["^d]/g,") "onbeforepaste= “('text',('text').replace(/[^d]/g,"))"

Use regular expressions to limit only numeric and English: onkeyup=”value=”/blog/(/[W]/g,””) “onbeforepaste=”('text',('text').replace(/[^d]/g,”