Summary of the 20 most commonly used regular expressions

In daily development, regular expressions are very useful. Knowing some commonly used regular expressions can greatly improve your work efficiency, such as string matching, format verification of form items, etc. Today I will share 20 of them with you. Commonly used regular expressions in development! ! ! Hope to help you improve code efficiency


  1. Verification of mobile phone number

    const phoneReg = ^\+[1-9]{1}[0-9]{7,11}$    
    const phoneStr1 = '18888888888'
    console.log(phoneReg.test(phoneStr1)) // true
    

  2. Verification of ID card

    const sfzReg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
    
    const sfzStr1 = '415106199801012130'
    console.log(sfzReg.test(sfzStr1)) // true
    
    const sfzStr2 = '718381298381212183'
    console.log(sfzReg.test(sfzStr2)) // false
    

  3. Email verification

    const emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/
    

  4. URL verification

    const urlReg = /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
    
    const urlStr1 = 'https://haha.fryao.com/xxx/xxx'
    console.log(urlReg.test(urlStr1)) // true
    
    const urlStr2 = 'sss://haha.fryao.com/xxx/xxx'
    console.log(urlReg.test(urlStr2)) // false
    

  5. IPv4 verification

    const ipv4Reg = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
    
    const ipv4Str1 = '122.12.56.65'
    console.log(ipv4Reg.test(ipv4Str1)) // true
    
    const ipv4Str2 = '122.12.56.655'
    console.log(ipv4Reg.test(ipv4Str2)) // false
    

  6. Verification of hexadecimal color

    const color16Reg = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/
    
    const color16Str1 = '#fff'
    console.log(color16Reg.test(color16Str1)) // true
    
    const color16Str2 = '#1234567'
    console.log(color16Reg.test(color16Str2)) // false
    

  7. Date YYYY-MM-DD

    const dateReg = /^\d{4}(\-)\d{1,2}\1\d{1,2}$/
    
    const dateStr1 = '2022-11-10'
    console.log(dateReg.test(dateStr1)) // true
    
    const dateStr2 = '2022-01-01 1'
    console.log(dateReg.test(dateStr2)) // false
    

  8. Date YYYY-MM-DD hh:mm:ss

    const dateReg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/
    
    const dateStr1 = '2022-11-10 16:16:16'
    console.log(dateReg.test(dateStr1)) // true
    
    const dateStr2 = '2022-11-10 16:'
    console.log(dateReg.test(dateStr2)) // false
    

  9. Integer verification

    const intReg = /^[-+]?\d*$/
    
    const intNum1 = 12345
    console.log(intReg.test(intNum1)) // true
    
    const intNum2 = 12345.1
    console.log(intReg.test(intNum2)) // false
    

  10. Checking of decimals

    const floatReg = /^[-\+]?\d+(\.\d+)?$/
    
    const floatNum = 1234.5
    console.log(floatReg.test(floatNum)) // true
    

  11. Keep n decimal places

    function checkFloat(n) {
       return new RegExp(`^([1-9]+[\d]*(.[0-9]{1,${n}})?)$`)
    }
    // retain 2 decimal places
    const floatReg = checkFloat(2)
    
    const floatNum1 = 1234.5
    console.log(floatReg.test(floatNum1)) // true
    
    const floatNum2 = 1234.55
    console.log(floatReg.test(floatNum2)) // true
    
    const floatNum3 = 1234.555
    console.log(floatReg.test(floatNum3)) // false
    

  12. Verification of QQ number Description: 5-11 digits: const qqReg = /^[1-9][0-9]{4,10}$/

    const qqStr1 = '1915801633'
    console.log(qqReg.test(qqStr1)) // true
    
    const qqStr2 = '191580163333'
    console.log(qqReg.test(qqStr2)) // false
    

  13. Micro-signal verification
    Description: 6 to 20 digits, starting with a letter, letter, number, minus sign, underscore:

    const wxReg = /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/
    
    const wxStr1 = 'linsanxin885577'
    console.log(wxReg.test(wxStr1)) // true
    
    const wxStr2 = '!vx'
    console.log(wxReg.test(wxStr2)) // false
    

  14. Strings containing only letters

    const letterReg = /^[a-zA-Z]+$/
    
    const letterStr1 = 'fryao'
    console.log(letterReg.test(letterStr1)) // true
    
    const letterStr2 = 'f_yao'
    console.log(letterReg.test(letterStr2)) // false
    

  15. Strings containing Chinese

    const cnReg = /[\u4E00-\u9FA5]/
    
    const cnStr1 = '我是fryao,方日尧'
    console.log(cnReg.test(cnStr1)) // true
    
    const cnStr2 = 'fr_yao'
    console.log(cnReg.test(cnStr2)) // false
    

  16. Verification of password strength Note: The password must contain letters, numbers, and special characters, at least 8 characters, and a maximum of 30 characters

    const passwordReg = /(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,30}/
    
    const password1 = 'f_ryao12345..'
    console.log(passwordReg.test(password1)) // true
    
    const password2 = 'fryao12345'
    console.log(passwordReg.test(password2)) // false
    

  17. Verification of string length n

    function checkStrLength(n) {
       return new RegExp(`^.{${n}}$`)
    }
    
    // check string with length 3
    const lengthReg = checkStrLength(3)
    
    const str1 = 'hhh'
    console.log(lengthReg.test(str1)) // true
    
    const str2 = 'hhhhh'
    console.log(lengthReg.test(str2)) // false
    

  18. Verification of file extension

    function checkFileName (arr) {
      arr = arr.map(name => `.${name}`).join('|')
      return new RegExp(`(${arr})$`)
    }
    
    const filenameReg = checkFileName(['jpg', 'png', 'txt'])
    
    const filename1 = 'fryao.jpg'
    console.log(filenameReg.test(filename1)) // true
    const filename2 = 'fryao.txt'
    console.log(filenameReg.test(filename2)) // true
    const filename3 = 'fryao.md'
    console.log(filenameReg.test(filename4)) // false
    

  19. Match comments in html

    const noteReg = /<!--(.*?)-->/g
    
    const htmlStr = '<!--a div tag--> <div></div> <!--a div tag--> <div></div>'
    
    console.log(noteReg.exec(htmlStr))
    //[
    // '<!--a div tag -->',
    // 'A div tag',
    // index: 0,
    // input: '<!--a div tag--> <div></div> <!--a div tag--> <div></div>',
    // groups: undefined
    // ]
    console.log(noteReg.exec(htmlStr))
    //[
    // '<!--a div tag -->',
    // 'A div tag',
    // index: 27,
    // input: '<!--a div tag--> <div></div> <!--a div tag--> <div></div>',
    // groups: undefined
    // ]
    

  20. Match the style in html

    const styleReg = /style="[^=>]*"([(\s+\w+=)|>])/g
    
    const htmlStr = '<div style="background:#000;"><span style="color:#fff"></span></div>'
    
    console.log(styleReg.exec(htmlStr))
    // [
    //   'style="background:#000;">',
    //   '>',
    //   index: 5,
    //   input: '<div style="background:#000;"><span style="color:#fff"></span></div>',
    //   groups: undefined
    // ]
    console.log(styleReg.exec(htmlStr))
    // [
    //   'style="color:#fff">',
    //   '>',
    //   index: 36,
    //   input: '<div style="background:#000;"><span style="color:#fff"></span></div>',
    //   groups: undefined
    // ]
    

Popular posts from this blog

大学资料分享——广工计院各个科目实验报告、课设及期末复习资料!!

JAVA Traffic Signal Light Course Design (Source Code + Report + Video Demonstration)

Win10 远程计算机或设备将不接受连接