js various regular expressions

1. nonnegative integer / ^ \ d + $ /

2. positive integer / ^ [0-9] [1-9] [0-9] $ /

3. Non-integer / ^ ((- \ d +) | (0 +)) $ /

4. negative integer / ^ - [0-9] [1-9] [0-9] $ /

5. integer / ^ -? \ D + $ /

6. Non-negative float /^\d+(.\d+)?$/

7. positive浮点number /^(([0-9]Tasu.[0-9] [1-9] [0-9] ) | ([0-9] [1-9] [0-9] . [0-9] Tasu) | ([0-9] [1-9] [0-9] )) $ /

8. Non-positive float /^((-\d+(.\d+)?)|(0+(.0+)?))$/

9.负浮score /^(-(([0-9]Tasu.[0-9] [1-9] [0-9] ) | ([0-9] [1-9] [0-9 ] . [0-9] Tasu) | ([0-9] [1-9] [0-9] ))) $ /

10. Float /^(-?\d+)(.\d+)?$/

11. Digital /^\d+(.{1}\d+)?$/

12. The string consists of the 26 English letters / ^ [A-Za-z] + $ /

13. The string of uppercase letters 26 composed of / ^ [AZ] + $ /

14. The string of 26 lowercase letters consisting of / ^ [az] + $ /

15. The string of numbers and English letters of 26 / ^ [A-Za-z0-9] + $ /

16. The string of digits, letters or underscore 26 composed of / ^ \ w + $ /

17. The string matches all characters composed of single-byte length / ^ [\ x00- \ xff] + $ /

18. The string matches all characters composed of two-byte length / ^ [^ \ x00- \ xff] + $ /

19. A double-byte character string is / [^ \ x00- \ xff] + /

20.email address /^[\w-]+(.[\w-]+)*@[\w-]+(.[\w-]+)+$/

 或者                      /w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*/

21.url address / ^ [A-zA-Z] +: - // (\ W + (\ W +) (. (\ W + (- \ + W)) )) (? \ \ S ) $ /?

 或者                      /http://([w-]+.)+[w-]+(/[w- ./?%&=]*)?/

22. Chinese characters matching the regular / [u4e00-u9fa5] /

23. Match double-byte characters (including characters) / [^ x00-xff] /

      应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
         String.prototype.len=function(){
             return this.replace([^x00-xff]/g,"aa").length;
         }

24. regular matching blank line / n [s |] * r /

25. regular HTML tags matching / <(. )>. | <(. *) /> /

26. match trailing spaces regular / (^ S ) | (S $) /

     应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下:
         String.prototype.trim = function(){
             return this.replace(/(^s*)|(s*$)/g, "");
         }

27. The matching IP address regular /(d+).(d+).(d+).(d+)/

     应用:利用正则表达式匹配IP地址,并将IP地址转换成对应数值的Javascript程序:
         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!");
             }
         }
     其实直接用split函数来分解可能更简单,程序如下:
         var ip="10.100.20.168";
         ip=ip.split(".");
         alert("IP值是:"+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1));

28. remove the javascript program string of repeating characters

    var s="abacabefgeeii";
     var s1=s.replace(/(.).*1/g,"$1");
     var re=new RegExp("["+s1+"]","g");
     var s2=s.replace(re,"");
     alert(s1+s2);                     //结果为:abcefgi

 /*使用后向引用取出包括重复的字符,再以重复的字符建立第二个表达式,取到不重复的字符,
   两者串连。这个方法对于字符顺序有要求的字符串可能不适用。*/

29. The use regular expressions to extract the file name from the URL address javascript program

     s="http://www.9499.net/page1.htm";
     s=s.replace(/(.*/){0,}([^.]+).*/ig,"$2");
     alert(s);                             //结果为page1
30.限制表单文本框输入内容

    只能输入中文:
         onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')" 
             onbeforepaste="clipboardData.setData('text',
             clipboardData.getData('text').replace(/[^u4E00-u9FA5]/g,''))"

    只能输入全角字符:
         onkeyup="value=value.replace(/[^uFF00-uFFFF]/g,'')" 
             onbeforepaste="clipboardData.setData('text',
             clipboardData.getData('text').replace(/[^uFF00-uFFFF]/g,''))"

    只能输入数字:
         onkeyup="value=value.replace(/[^d]/g,'')" 
             onbeforepaste="clipboardData.setData('text',
             clipboardData.getData('text').replace(/[^d]/g,''))"

    只能输入数字和英文:
         onkeyup="value=value.replace(/[W]/g,'')" 
             onbeforepaste="clipboardData.setData('text',
             clipboardData.getData('text').replace(/[^d]/g,''))"

31. Verify that the file names of letters, numbers, down the line composed /^((\w+)(.{1})(\w+))$/

32. match the date (1900-1999)

     /^19\d{2}-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2][0-9])|(3([0|1])))$/

33. match the 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 and 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}))?$/

rule

  • Integer or decimal:. ^ [0-9] {0,1} + [0-9] {0,2} $
  • Enter numbers only: "^ [0-9] * $."
  • Only n-bit digital input: "^ \ d {n} $".
  • Only input digital bits at least n: "^ \ d {n,} $".
  • M ~ n can only enter the number of bits: "^ \ D {m, n} $"
  • You can only enter a zero and non-zero number at the beginning: "^ (0 | [1-9] [0-9] *) $".
  • There are only two positive real number input decimal: "(. [0-9] {2})? ^ [0-9] + $".
  • 1 to enter only three decimal positive real number: "(. [0-9] {1,3})? ^ [0-9] + $".
  • You can only enter a non-zero positive integers: "? ^ + [1-9] [0-9] * $."
  • You can only enter a non-zero negative integer: "^ - [1-9] [] 0-9" $ *.
  • Only 3 of the length of the input characters:. "$ ^ {3}."
  • 26 only by the input string of letters in English: "^ [A-Za-z] + $".
  • You can only enter a string of 26 English capital letters: "^ [AZ] + $".
  • You can only enter letters written by a string consisting of 26 small: "^ [az] + $".
  • Only input string of numbers and English letters 26: "^ [A-Za-z0-9] + $".
  • You can only enter the numbers, 26 English letters or underscore the string: "^ \ w + $".
  • Verify User Password: "^ [a-zA-Z] \ w {5,17} $" The correct format is: begin with a letter, a length between 6 and 18, only contain characters, numbers and underscores.
  • Verify that contain ^% & '?,; = $ \ "Characters such as:" [^% &' ',; = $ \ x22?] + ".
  • Can input Chinese characters: "^ [\ u4e00- \ u9fa5] {0,} $"
  • 验证Email地址:"^\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)*$"。
  • 验证InternetURL:"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"。
  • Verify that the phone number: "^ ((\ d {3,4} -) | \ d {3.4} -) \ d {7,8} $?" The correct format is: "XXX-XXXXXXX", "XXXX- XXXXXXXX" , "XXX-XXXXXXX", "XXX-XXXXXXXX", "XXXXXXX" and "XXXXXXXX".
  • ID verification number (15 or 18 digits): "^ \ d {15} | \ d {18} $".
  • Verify year 12 months: "^ (0 [1-9] | 1 [0-2]?) $" Correct format: "01" through "09" and "1" to "12."
  • Verify month 31 days: "^ (? (0 [1-9]) | ((1 | 2) [0-9]) | 30 | 31) $" correct format; "01" through "09" and "1" to "31." Integer or decimal:. ^ [0-9] {0,1} + [0-9] {0,2} $
  • Enter numbers only: "^ [0-9] * $."
  • Only n-bit digital input: "^ \ d {n} $".
  • Only input digital bits at least n: "^ \ d {n,} $".
  • M ~ n can only enter the number of bits: "^ \ D {m, n} $"
  • You can only enter a zero and non-zero number at the beginning: "^ (0 | [1-9] [0-9] *) $".
  • There are only two positive real number input decimal: "(. [0-9] {2})? ^ [0-9] + $".
  • 1 to enter only three decimal positive real number: "(. [0-9] {1,3})? ^ [0-9] + $".
  • You can only enter a non-zero positive integers: "? ^ + [1-9] [0-9] * $."
  • You can only enter a non-zero negative integer: "^ - [1-9] [] 0-9" $ *.
  • Only 3 of the length of the input characters:. "$ ^ {3}."
  • 26 only by the input string of letters in English: "^ [A-Za-z] + $".
  • You can only enter a string of 26 English capital letters: "^ [AZ] + $".
  • You can only enter letters written by a string consisting of 26 small: "^ [az] + $".
  • Only input string of numbers and English letters 26: "^ [A-Za-z0-9] + $".
  • You can only enter the numbers, 26 English letters or underscore the string: "^ \ w + $".
  • Verify User Password: "^ [a-zA-Z] \ w {5,17} $" The correct format is: begin with a letter, a length between 6 and 18, only contain characters, numbers and underscores.
  • Verify that contain ^% & '?,; = $ \ "Characters such as:" [^% &' ',; = $ \ x22?] + ".
  • Can input Chinese characters: "^ [\ u4e00- \ u9fa5] {0,} $"
  • 验证Email地址:"^\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)*$"。
  • 验证InternetURL:"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"。
  • Verify that the phone number: "^ ((\ d {3,4} -) | \ d {3.4} -) \ d {7,8} $?" The correct format is: "XXX-XXXXXXX", "XXXX- XXXXXXXX" , "XXX-XXXXXXX", "XXX-XXXXXXXX", "XXXXXXX" and "XXXXXXXX".
  • ID verification number (15 or 18 digits): "^ \ d {15} | \ d {18} $".
  • Verify year 12 months: "^ (0 [1-9] | 1 [0-2]?) $" Correct format: "01" through "09" and "1" to "12."
  • Verify month 31 days: "^ (? (0 [1-9]) | ((1 | 2) [0-9]) | 30 | 31) $" correct format; "01" through "09" and "1" to "31."

Guess you like

Origin www.cnblogs.com/szj-/p/11703803.html