Front-end tips for daily use
Commonly used regular finishing
Commonly used regular
effect | expression | Remarks |
---|---|---|
Match Chinese characters | [u4e00-u9fa5] |
|
Match double bytes | [^x00-xff] |
One double-byte character length meter 2 and ASCII character meter 1 |
Match blank lines | ns*r |
Can be used to delete blank lines |
Match HTML tags | < (S*?)[^>]*>.*?|< .*? /> |
Can only match parts, not nested matches |
Match whitespace characters | ^s*|s*$ |
|
Email address | w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)* |
|
URL URL | [a-zA-z]+://[^s]* |
|
Matching account | ^[a-zA-Z][a-zA-Z0-9_]{4,15}$ |
Begin with letters, allow 5-16 bytes, allow alphanumeric underscores |
Domestic landline number | d{3}-d{8}|d{4}-d{7} |
Example: 021-87888822 |
QQ number | [1-9][0-9]{4,} |
|
China Postcode | [1-9]d{5}(?!d) |
|
Chinese ID card | d{15}|d{18} |
Not very accurate, does not include the ID card with X |
Match ip address | d+.d+.d+.d+ |
Match specific digits
effect | expression |
---|---|
Positive integer | ^[1-9]d*$ |
Negative integer | ^-[1-9]d*$ |
Integer | ^-?[1-9]d*$ |
Non-negative integer (positive integer + 0) | ^[1-9]d*|0$ |
Non-positive integer (negative integer + 0) | ^-[1-9]d*|0$ |
Number of positive floats | ^[1-9]d*.d*|0.d*[1-9]d*$ |
Number of floating points | ^-([1-9]d*.d*|0.d*[1-9]d*)$ |
Floating point | ^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$ |
Non-floating point number (positive floating point number + 0) | ^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$ |
Non-standard floating point (negative floating point + 0) | ^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$ |
digital | ^[0-9]*$ |
n digits | ^d{n}$ |
At least n digits | ^d{n,}$ |
mn digits | ^d{m,n}$ |
Numbers starting with zero and non-zero | ^(0|[1-9][0-9]*)$ |
Positive real number with two decimal places | ^[0-9]+(.[0-9]{2})?$ |
Positive real numbers with 1-3 decimal places | ^[0-9]+(.[0-9]{1,3})?$ |
Non-zero positive integer | ^+?[1-9][0-9]*$ |
Non-zero negative integer | ^-[1-9][0-9]*$ |
Characters of length 3 | ^.{3}$ |
A string of 26 English letters | ^[A-Za-z]+$ |
A string of 26 uppercase English letters | ^[A-Z]+$ |
A string consisting of numbers and 26 English letters | ^[A-Za-z0-9]+$ |
A string consisting of numbers, 26 English letters or underscores | ^w+$ |
Password: start with a letter and be between 6-18 | ^[a-zA-Z]w{5,17}$ |
Other operations
Application: Use regular expressions to decompose and translate IP addresses
function IP2V(ip){ //IP地址转换成对应数值
re=/(d+).(d+).(d+).(d+)/g //匹配IP地址的正则表达式
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!”)
}
}
Note: The above codes are collected and sorted on the network itself, only for the convenience of finding records.