1. Non-negative integer/^\d+$/
2. Positive integer/^[0-9]*[1-9][0-9]*$/
3. Non-positive integer/^ ((-\d+) | ( 0+)) $/
4. Negative integer/^-[0-9]*[1-9][0-9]*$/
5. Integer/^-?\d+$/
6. Non-negative floating point/^\d+ (\.\d+)? $/
7. Positive floating-point number/^ ([0-9]+\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*)) $/
8. Non-positive floating-point number/^ ((-\d+ (\.\d+)?) | (0+ (\.0+)?)) $/
9. Negative floating-point number/^ (-([0-9]+\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*))) $/
10. Floating point/^ (-?\d+) (\.\d+)? $/
11. Digital/^\d+ (\.{ 1}\d+)? $/
12. A string consisting of 26 English letters/^[a-za-z]+$/
13. A string consisting of 26 uppercase letters of the English alphabet/^[a-z]+$/
14. A string consisting of 26 letters in lowercase/^[a-z]+$/
15. A string consisting of a number and 26 English letters/^[a-za-z0-9]+$/
16. A string of numbers, 26 English letters, or underscores/^\w+$/
17. Match all characters of single-byte-length character string/^[\x00-\xff]+$/
18. Match all characters of the double-byte length string/^[^\x00-\xff]+$/
19. Does the string contain double-byte characters/[^\x00-\xff]+/
20.email Address/^[\w-]+ (\.[ \w-]+) *@[\w-]+ (\.[ \w-]+) +$/
or/w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) */
21.url Address/^[a-za-z]+://(\w+ (-\w+) *) (\. ( \w+ (-\w+) *) * (\?\s*) $/
or/http://([w-]+.) +[w-]+ (/[w-./?%&=]*)?
22. Regular/[u4e00-u9fa5]/for matching Chinese characters
23. Match double-byte characters (including Chinese characters)/[^x00-xff]/
Application: Calculates the length of a string (a double-byte character length meter 2,ascii character 1)
String.prototype.len=function () {
Return This.replace ([^x00-xff]/g, "AA"). Length;
}
24. Match the regular/n[s| of the blank line] *r/
25. Regular/< (. *) Matching HTML tags >.*</1>|< (. *)/>/
26. Matches the regular/(^s*) of the leading and trailing spaces | (s*$)/
Application: There is no trim function like VBScript in JavaScript, and we can use this expression to do the following:
String.prototype.trim = function () {
Return This.replace (/(^s*) | ( s*$)/g, "");
}
27. Match the regular/(d+) of the IP address. (d+). (d+). (d+)/
application: Use regular expressions to match IP addresses and convert IP addresses to corresponding numeric JavaScript programs:
function IP2V (IP) {
re=/(d+). (d+). (d+). (d+)/g;
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!");
}
}
in fact, directly using the split function to decompose may be simpler, the program is as follows:
var ip= "10.100.20.168";
ip=ip.split (".");
alert ("IP value is:" + (ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1));
28. JavaScript programs that remove duplicate characters from a string
var s= "Abacabefgeeii";
var s1=s.replace (/(.). *1/g, "$");
var re=new RegExp ("[" +s1+ "]", "G");
var s2=s.replace (Re, "");
alert (S1+S2); The result is: ABCEFGI
/* Use a post-reference to include duplicate characters, then create a second expression with duplicate characters, and take the characters that are not duplicated.
A concatenation of the two. This method may not be applicable for strings that require a character order. */
29. JavaScript program to extract file names from URL addresses using regular expressions
S= "http://www.9499.net/page1.htm";
S=s.replace (/(. */) {0,} ([^.] +). */ig, "$");
alert (s); The result is Page1
30. Restrict Form text box entry
can only be entered in Chinese:
onkeyup= "value= Value.replace (/[^u4e00-u9fa5]/g, ') "&NBSP;
onbeforepaste= "Clipboarddata.setdata (' Text ',
clipboarddata.getdata (' text '). Replace (/[^u4e00-u9fa5]/g, ') "
can only enter full-width characters:
onkeyup= "value= Value.replace (/[^uff00-uffff]/g, ') "&NBSP;
onbeforepaste= "Clipboarddata.setdata (' Text ',
clipboarddata.getdata (' text '). Replace (/[^uff00-uffff]/g, ') "
can only enter numbers:
onkeyup= "value= Value.replace (/[^d]/g, ') "&NBSP;
onbeforepaste= "clipboarddata.setdata (' text ',
clipboarddata.getdata (' text '). Replace (/[^d]/g, ') "
Only numbers and English can be entered:
Onkeyup= "Value=value.replace (/[w]/g, ')"
Onbeforepaste= "Clipboarddata.setdata (' text ',
Clipboarddata.getdata (' text '). Replace (/[^d]/g, ')) "
31. Verify that the file name consists of letters, numbers, and/^ (\w+) (\.{ 1}) (\w+)) $/
32. Match date (1900-1999)
/^19\d{2}-((0[1-9)) | ( 1[0-2])-((0[1-9)) | ( [1-2] [0-9]) | (3 ([0|1]))) $/
33. Match Date (2000-2999)
/^20\d{2}-((0[1-9)) | ( 1[0-2])-((0[1-9)) | ( [1-2] [0-9]) | (3 ([0|1]))) $/
34. Match Date Time
/^ (1|2\d{3}-(0[1-9]) | ( 1[0-2])-((0[1-9)) | ( [1-2] [0-9]) | (3 ([0|1]))) ((\d{2}):(\d{2}):(\d{2})? $/
JS commonly used in various regular expressions 1