jquery Regular Expressions

Source: Internet
Author: User
Tags pow

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, "$1″);

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, "$2″);

alert (s); The result is Page1

30. Restrict Form text box entry

You can only enter Chinese:

Onkeyup= "Value=value.replace (/[^u4e00-u9fa5]/g,") "

Onbeforepaste= "Clipboarddata.setdata (' text ',

Clipboarddata.getdata (' text '). Replace (/[^u4e00-u9fa5]/g, "))"

Only full-width characters can be entered:

Onkeyup= "Value=value.replace (/[^uff00-uffff]/g,") "

Onbeforepaste= "Clipboarddata.setdata (' text ',

Clipboarddata.getdata (' text '). Replace (/[^uff00-uffff]/g, "))"

Only numbers can be entered:

Onkeyup= "Value=value.replace (/[^d]/g,") "

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})? $/

jquery Regular Expressions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.