Commonly used C# regular expressions!
"^\d+$" //Non-negative integer (positive integer + 0)
"^[0-9]*[1-9][0-9]*$" //Positive integer
"^((-\d+)|(0+))$" //Non-positive integer (negative integer + 0)
"^-[0-9]*[1-9][0-9]*$" //Negative integer
"^-?\d+$" //Integer
"^\d+(\.\d+)?$" //Non-negative floating point number (positive floating point number + 0)
"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]+)|([0-9]*[1-9][0-9]*))$" //Positive floating point number
"^((-\d+(\.\d+)?)|(0+(\.0+)?))$" //Non-positive floating point number (negative floating point number + 0)
"^(-((([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]+)|([0-9]*[1-9][0-9]*)))$" //Negative floating point number
"^(-?\d+)(\.\d+)?$" //Floating point number
"^[A-Za-z]+$" // A string composed of 26 English letters
"^[A-Z]+$" //A string composed of 26 English letters capitalization
"^[a-z]+$" //A string composed of 26 English letters lowercase
"^[A-Za-z0-9]+$" // A string composed of numbers and 26 English letters
"^\w+$" //A string composed of numbers, 26 English letters or underscores
"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$" //email address
"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$" //url
/^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/// / Year-Month-Day
/^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/// Month/Day/Year
"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$" //Emil
"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?" //Phone Number
"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$" //IP address
YYYY-MM-DD basically takes into account the situations of leap years and February.
^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$
C# regular expression
Picture src[^>]*[^/].(?:jpg|bmp|gif)(?:\"|\')
Chinese ^([\u4e00-\u9fa5]+|[a-zA-Z0-9]+)$
URL "\<a.+?href=['""](?!http\:\/\/)(?!mailto\:)(?>foundAnchor>[^'"">]+?)[^>]*?\>"
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: /<(.*)>.*<\/\1>|<(.*) \//
Regular expression matching the beginning and end spaces: (^\s*)|(\s*$) (trim function like vbscript)
Regular expression matching the email address: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Regular expression matching URL: http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
---------------------------------------------------------------------------
Here are examples:
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=(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="('text',('text').replace(/[^\u4E00-\u9FA5]/g,''))"
1. Use regular expressions to limit only full-width characters to enter: onkeyup="value=(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="('text',('text').replace(/[^\uFF00-\uFFF]/g,''))"
2. Use regular expressions to limit only numbers to be entered: onkeyup="value=(/[^\d]/g,'')"onbeforepaste="('text',('text').replace(/[^\d]/g,''))"
3. Use regular expressions to limit only numerical and English: onkeyup="value=(/[\W]/g,'')"onbeforepaste="('text',('text').replace(/[^\d]/g,''))"
4. Calculate the length of the string (one double-byte character length meter 2, ASCII character meter 1)
=function(){return ([^\x00-\xff]/g,"aa").length;}
There is no trim function like vbscript in it, so we can use this expression to implement it, as follows:
= function()
{
return (/(^\s*)|(\s*$)/g, "");
}
Decompose and convert IP addresses using regular expressions:
6. The following is a Javascript program that uses regular expressions to match the IP address and converts the IP address into the corresponding value:
function IP2V(ip)
{
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 correct IP address!")
}
}
However, if the above program does not use regular expressions, but uses the split function to decompose it directly. The program is as follows:
var ip="10.100.20.168"
ip=(".")
alert("IP value is: "+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))
(?<=>)[^>]*(?=<)
"^\d+$" //Non-negative integer (positive integer + 0)
"^[0-9]*[1-9][0-9]*$" //Positive integer
"^((-\d+)|(0+))$" //Non-positive integer (negative integer + 0)
"^-[0-9]*[1-9][0-9]*$" //Negative integer
"^-?\d+$" //Integer
"^\d+(\.\d+)?$" //Non-negative floating point number (positive floating point number + 0)
"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]+)|([0-9]*[1-9][0-9]*))$" //Positive floating point number
"^((-\d+(\.\d+)?)|(0+(\.0+)?))$" //Non-positive floating point number (negative floating point number + 0)
"^(-((([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]+)|([0-9]*[1-9][0-9]*)))$" //Negative floating point number
"^(-?\d+)(\.\d+)?$" //Floating point number
"^[A-Za-z]+$" // A string composed of 26 English letters
"^[A-Z]+$" //A string composed of 26 English letters capitalization
"^[a-z]+$" //A string composed of 26 English letters lowercase
"^[A-Za-z0-9]+$" // A string composed of numbers and 26 English letters
"^\w+$" //A string composed of numbers, 26 English letters or underscores
"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$" //email address
"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$" //url
/^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/// / Year-Month-Day
/^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/// Month/Day/Year
"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$" //Emil
"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?" //Phone Number
"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$" //IP address
YYYY-MM-DD basically takes into account the situations of leap years and February.
^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$
C# regular expression
Picture src[^>]*[^/].(?:jpg|bmp|gif)(?:\"|\')
Chinese ^([\u4e00-\u9fa5]+|[a-zA-Z0-9]+)$
URL "\<a.+?href=['""](?!http\:\/\/)(?!mailto\:)(?>foundAnchor>[^'"">]+?)[^>]*?\>"
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: /<(.*)>.*<\/\1>|<(.*) \//
Regular expression matching the beginning and end spaces: (^\s*)|(\s*$) (trim function like vbscript)
Regular expression matching the email address: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Regular expression matching URL: http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
---------------------------------------------------------------------------
Here are examples:
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=(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="('text',('text').replace(/[^\u4E00-\u9FA5]/g,''))"
1. Use regular expressions to limit only full-width characters to enter: onkeyup="value=(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="('text',('text').replace(/[^\uFF00-\uFFF]/g,''))"
2. Use regular expressions to limit only numbers to be entered: onkeyup="value=(/[^\d]/g,'')"onbeforepaste="('text',('text').replace(/[^\d]/g,''))"
3. Use regular expressions to limit only numerical and English: onkeyup="value=(/[\W]/g,'')"onbeforepaste="('text',('text').replace(/[^\d]/g,''))"
4. Calculate the length of the string (one double-byte character length meter 2, ASCII character meter 1)
=function(){return ([^\x00-\xff]/g,"aa").length;}
There is no trim function like vbscript in it, so we can use this expression to implement it, as follows:
= function()
{
return (/(^\s*)|(\s*$)/g, "");
}
Decompose and convert IP addresses using regular expressions:
6. The following is a Javascript program that uses regular expressions to match the IP address and converts the IP address into the corresponding value:
function IP2V(ip)
{
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 correct IP address!")
}
}
However, if the above program does not use regular expressions, but uses the split function to decompose it directly. The program is as follows:
var ip="10.100.20.168"
ip=(".")
alert("IP value is: "+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))
(?<=>)[^>]*(?=<)