JS commonly used various regular expressions

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 number /^\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 float/^((-\d+(\.\d+)?)|(0+(\.0+)?))$/ 9.negative

float/^(-(([0-9 ]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+) |([0-9]*[1-9][0-9]*)))$/

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

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

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

13. A string consisting of uppercase 26 English letters /^[AZ]+$/

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

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

16. A string consisting of numbers, 26 English letters or underscores /^\w+$/

17. Match all single-byte characters in a string /^[\x00-\xff]+$/

18. Match all double-byte characters in a string /^[^\x00-\xff]+$ /

19. Whether the string contains double-byte words /[^\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 matching Chinese characters /[u4e00-u9fa5]/

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

          Application: Calculate the length of a string (the length of a double-byte character counts as 2, and the length of an ASCII character counts as 1)
             String.prototype.len=function(){
                 return this.replace([^x00-xff]/g,"aa") .length;
             }

24. Regular for matching blank lines /n[s| ]*r/

25. Regular for matching HTML tags /<(.*)>.*</1>|<(.*) />/

26 .Regular /(^s*)|(s*$)/

         application that matches the leading and trailing spaces: There is no trim function like vbscript in javascript, we can use this expression to achieve it, as follows:
             String.prototype.trim = function (){
                 return this.replace(/(^s*)|(s*$)/g, "");
             }

27. Regular IP address matching /(d+).(d+).(d+).(d+ )/

         application: a Javascript program that uses regular expressions to match IP addresses and converts IP addresses to corresponding values:
             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, it may be better to use the split function directly to decompose Simple, the procedure 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. A javascript program to remove repeated characters in a string

        var s="abacaefgeeii";
         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 backreference to extract characters including repeated characters, and then create a second expression with repeated characters to get non-repeated characters,
       two are connected in series. This method may not be suitable for strings with character order requirements. */

29. A javascript program that uses regular expressions to extract file names from URL addresses

         s="http://www.9499.net/page1.htm";
         s=s.replace(/(.*/){0 ,}([^.]+).*/ig,"$2");
         alert(s); //The result is page1

     30. Limit the input content of the form text box to

        only Chinese:
             onkeyup="value=value.replace (/[^u4E00-u9FA5]/g,'')"
                 onbeforepaste="clipboardData.
                 clipboardData.getData('text').replace(/[^u4E00-u9FA5]/g,''))"

        can only input full-width characters:
             onkeyup="value=value.replace(/[^uFF00-uFFFF]/g ,'')"
                 onbeforepaste="clipboardData.setData('text',
                 clipboardData.getData('text').replace(/[^uFF00-uFFFF]/g,''))"

        Only enter numbers:
             onkeyup=" value=value.replace(/[^d]/g,'')"
                 onbeforepaste="clipboardData.setData('text',
                 clipboardData.getData('text').replace(/[^d]/g,'' ))"

        can only enter numbers and English:
             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 underscores /^((\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 datetime

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

function checkQty(obj) {
    console.log(obj.value);
    var regExp = new RegExp(/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/);
    var result = regExp.test(obj.value);
    console.log(result);
}


Integer or decimal: ^[0-9]+\.{0,1}[0-9]{0,2}$
Only numbers can be entered: "^[0-9]*$".
Only n-digit numbers can be entered: "^\d{n}$".
Only numbers with at least n digits can be entered: "^\d{n,}$".
You can only enter numbers with m~n digits: . "^\d{m,n}$"
Only numbers starting with zero and non-zero can be entered: "^(0|[1-9][0-9]*)$".
Only positive real numbers with two decimal places can be entered: "^[0-9]+(.[0-9]{2})?$".
Only positive real numbers with 1 to 3 decimal places can be input: "^[0-9]+(.[0-9]{1,3})?$".
Only non-zero positive integers can be entered: "^\+?[1-9][0-9]*$".
Only non-zero negative integers can be entered: "^\-[1-9][]0-9"*$.
Only characters of length 3 can be entered: "^.{3}$".
You can only enter a string consisting of 26 English letters: "^[A-Za-z]+$".
You can only enter a string consisting of 26 uppercase English letters: "^[AZ]+$".
You can only enter a string consisting of 26 lowercase English letters: "^[az]+$".
You can only enter a string consisting of numbers and 26 English letters: "^[A-Za-z0-9]+$".
You can only enter a string consisting of numbers, 26 English letters or underscores: "^\w+$".
Verify user password: "^[a-zA-Z]\w{5,17}$" The correct format is: start with a letter, the length is between 6 and 18, and can only contain characters, numbers and underscores.
Verify that it contains characters such as ^%&'',;=?$\": "[^%&'',;=?$\x22]+".
Only Chinese characters can be input: "^[\u4e00-\u9fa5]{0,}$"
Verify Email Address: "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$".
验证InternetURL:"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"。
Verify 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".
Verify ID number (15 or 18 digits): "^\d{15}|\d{18}$".
Verify the 12 months of a year: "^(0?[1-9]|1[0-2])$" The correct format is: "01"~"09" and "1"~"12".
Verify the 31 days of a month: "^((0?[1-9])|((1|2)[0-9])|30|31)$" The correct format is; "01"~"09" and "1" to "31". Integer or decimal: ^[0-9]+\.{0,1}[0-9]{0,2}$
Only numbers can be entered: "^[0-9]*$".
Only n-digit numbers can be entered: "^\d{n}$".
Only numbers with at least n digits can be entered: "^\d{n,}$".
You can only enter numbers with m~n digits: . "^\d{m,n}$"
Only numbers starting with zero and non-zero can be entered: "^(0|[1-9][0-9]*)$".
Only positive real numbers with two decimal places can be entered: "^[0-9]+(.[0-9]{2})?$".
Only positive real numbers with 1 to 3 decimal places can be input: "^[0-9]+(.[0-9]{1,3})?$".
Only non-zero positive integers can be entered: "^\+?[1-9][0-9]*$".
Only non-zero negative integers can be entered: "^\-[1-9][]0-9"*$.
Only characters of length 3 can be entered: "^.{3}$".
You can only enter a string consisting of 26 English letters: "^[A-Za-z]+$".
You can only enter a string consisting of 26 uppercase English letters: "^[AZ]+$".
You can only enter a string consisting of 26 lowercase English letters: "^[az]+$".
You can only enter a string consisting of numbers and 26 English letters: "^[A-Za-z0-9]+$".
You can only enter a string consisting of numbers, 26 English letters or underscores: "^\w+$".
Verify user password: "^[a-zA-Z]\w{5,17}$" The correct format is: start with a letter, the length is between 6 and 18, and can only contain characters, numbers and underscores.
Verify that it contains characters such as ^%&'',;=?$\": "[^%&'',;=?$\x22]+".
Only Chinese characters can be input: "^[\u4e00-\u9fa5]{0,}$"
Verify Email Address: "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$".
验证InternetURL:"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"。
Verify 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".
Verify ID number (15 or 18 digits): "^\d{15}|\d{18}$".
Verify the 12 months of a year: "^(0?[1-9]|1[0-2])$" The correct format is: "01"~"09" and "1"~"12".
Verify the 31 days of a month: "^((0?[1-9])|((1|2)[0-9])|30|31)$" The correct format is; "01"~"09" and "1" to "31".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326212824&siteId=291194637