Below are some of the more commonly used regular expressions that I have collected, because they may be used more often during form validation. Specially issued for all friends to use together. hehe.
Regular expression to match Chinese characters: [u4e00-u9fa5]
Comment: Matching Chinese is really a headache. With this expression, it will be easier.
Match double-byte characters (including Chinese characters): [^x00-xff]
Comment: Can be used to calculate the length of a string (the length of a double-byte character counts as 2, and the length of an ASCII character counts as 1)
Regular expression to match blank lines: ns*r
Comment: Can be used to delete blank lines
Regular expression matching HTML tags: < (S*?)[^>]*>.*?|< .*? />
Comment: The version circulating on the Internet is too bad. The above one can only match part of it, and it is still powerless for complex nested tags.
Regular expression matching leading and trailing whitespace characters: ^s*|s*$
Comment: It can be used to delete whitespace characters (including spaces, tabs, form feeds, etc.) at the beginning and end of the line. It is a very useful expression.
Regular expression matching email addresses: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
Comment: Very useful for form validation
Regular expression to match URL: [a-zA-z]+://[^s]*
Comment: The version circulating on the Internet has very limited functions. The above one can basically meet the needs.
Is the matching account legal (starting with a letter, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Comment: Very useful for form validation
Match domestic phone numbers: d{3}-d{8}|d{4}-d{7}
Comment: Matching format such as 0511-4405222 or 021-87888822
Match Tencent QQ number: [1-9][0-9]{4,}
Comment: Tencent QQ account starts from 10000
Match Chinese postal code: [1-9]d{5}(?!d)
Comment: China’s postal code is a 6-digit number
Matching ID card: d{15}|d{18}
Comment: China’s ID card has 15 or 18 digits
Match ip address: d+.d+.d+.d+
Comment: Useful when extracting IP address
Match specific numbers:
^[1-9]d*$ //Match positive integers
^-[1-9]d*$ //Match negative integers
^-?[1-9]d*$ //Match integers
^[1-9]d*|0$ //Match non-negative integers (positive integers + 0)
^-[1-9]d*|0$ //Match non-positive integers (negative integers + 0)
^[1-9]d*.d*|0.d*[1-9]d*$ //Match positive floating point numbers
^-([1-9]d*.d*|0.d*[1-9]d*)$ //Match negative floating point numbers
^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$ //Match floating point numbers
^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$ //Match non-negative floating point numbers (positive floating point numbers + 0)
^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$ //Match non-positive floating point numbers (negative floating point numbers + 0 )
Comment: Useful when processing large amounts of data, please pay attention to corrections when applying it.
Match a specific string:
^[A-Za-z]+$ //Match a string consisting of 26 English letters
^[AZ]+$ //Match a string consisting of 26 uppercase English letters
^[az]+$ //Match a string consisting of 26 lowercase English letters
^[A-Za-z0-9]+$ //Match a string consisting of numbers and 26 English letters
^w+$ //Match a string consisting of numbers, 26 English letters or underscores
The verification function and its verification expression when using the RegularExpressionValidator verification control are introduced as follows:
Only numbers can be entered: "^[0-9]*$"
Only n-digit numbers can be entered: "^d{n}$"
You can only enter at least n digits: "^d{n,}$"
Only mn-digit numbers can be entered: "^d{m,n}$"
Only numbers starting with zero and non-zero can be 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 a length of 3 can be entered: "^.{3}$"
Only a string consisting of 26 English letters can be entered: "^[A-Za-z]+$"
Only a string consisting of 26 uppercase English letters can be entered: "^[AZ]+$"
You can only enter a string consisting of 26 lowercase English letters: "^[az]+$"
Only a string consisting of numbers and 26 English letters can be entered: "^[A-Za-z0-9]+$"
You can only enter a string consisting of numbers, 26 English letters or underscores: "^w+$"
Verify user password: "^[a-zA-Z]w{5,17}$" The correct format is: starting with a letter, the length is between 6-18,
Can only contain characters, numbers, and underscores.
Verify whether it contains characters such as ^%&',;=?$": "[^%&',;=?$x22]+"
Only Chinese characters can be entered: "^[u4e00-u9fa5],{0,}$"
Verify email address: "^w+[-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$"
Verify InternetURL: "^http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$"
Verification phone number: "^((d{3,4})|d{3,4}-)?d{7,8}$"
The correct format is: "XXXX-XXXXXXX", "XXXX-XXXXXXXX", "XXX-XXXXXXX",
"XXX-XXXXXXXX", "XXXXXXX", "XXXXXXXX".
Verify ID number (15 or 18 digits): "^d{15}|d{}18$"
Verify the 12 months of a year: "^(0?[1-9]|1[0-2])$" The correct format is: "01"-"09" and "1" "12"
Verify the 31 days of a month: "^((0?[1-9])|((1|2)[0-9])|30|31)$"
The correct format is: "01" "09" and "1" "31".
Regular expression to match Chinese characters: [u4e00-u9fa5]
Match double-byte characters (including Chinese characters): [^x00-xff]
Regular expression to match empty lines: n[s| ]*r
Regular expression matching HTML tags: /< (.*)>.*|< (.*) />/
Regular expression matching leading and trailing spaces: (^s*)|(s*$)
Regular expression matching email addresses: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
Regular expression to match URL: http://([w-]+.)+[w-]+(/[w- ./?%&=]*)?
(1) Application: Calculate the length of a string (the length of a double-byte character counts as 2, and the length of an ASCII character counts as 1)
String.prototype.len=function(){return this.replace([^x00-xff]/g,”aa”).length;}
(2) Application: There is no trim function like vbscript in JavaScript, so we can use this expression to achieve
String.prototype.trim = function()
{
return this.replace(/(^s*)|(s*$)/g, “”);
}
(3)Application: Use regular expressions to decompose and convert IP addresses
function IP2V(ip) //Convert IP address to corresponding value
{
re=/(d+).(d+).(d+).(d+)/g //Regular expression matching IP address
if(re.test(ip))
{
return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1
}
else
{
throw new Error("Not a valid IP address!")
}
}
(4)Application: javascript program that extracts file names from URL addresses
s=”//www.VeVB.COm/page1.htm”;
s=s.replace(/(.*/){0,}([^.]+).*/ig,”$2″); //Page1.htm
(5)Application: Use regular expressions to limit the input content of text boxes in web forms
Use regular expressions to limit input to Chinese only: onkeyup="value="/blog/value.replace(/["^u4E00-u9FA5]/g,") ” onbeforepaste="clipboardData.setData('text',clipboardData. getData('text').replace(/[^u4E00-u9FA5]/g,”))”
Use regular expressions to limit the input of only full-width characters: onkeyup="value="/blog/value.replace(/["^uFF00-uFFFF]/g,") ” onbeforepaste="clipboardData.setData('text',clipboardData .getData('text').replace(/[^uFF00-uFFFF]/g,"))"
Use regular expressions to limit input to numbers: onkeyup="value="/blog/value.replace(/["^d]/g,") "onbeforepaste="clipboardData.setData('text',clipboardData.getData( 'text').replace(/[^d]/g,"))"
Use regular expressions to limit input to numbers and English only: onkeyup="value="/blog/value.replace(/[W]/g,"") "onbeforepaste="clipboardData.setData('text',clipboardData.getData ('text').replace(/[^d]/g,"