[Note] เกี่ยวกับ Regular Expression(regex)

Aug. 23, 2024 · boychawin

ในส่วนนี้ผมทำไว้เพื่อเป็น Note เพื่อเก็บ Regular Expression (regex) นะครับ


    const mobileNoRegex: any = /^((0)[0-9]{9})$|^((88)[0-9]{8})$/g;
    const idCardRegex = /^[1-5|8-9]{1}[0-9]{12}/;
    const passportRegex = /^((?!.*(012345|123456|234567|345678|456789|000000|111111|222222|333333|444444|555555|666666|777777|888888|999999))([0-9A-Za-z]{1}[0-9]{5,7}[0-9A-Za-z]{0,1}|[0-9A-Za-z]{1}[0-9]{4}[0-9A-Za-z]{1}|[0-9A-Za-z]{2}[0-9]{3,6}[0-9A-Za-z]{1}|[0-9A-Za-z]{2}[0-9]{4,6}[0-9A-Za-z]{0,1}|[0-9]{2}[A-Za-z]{2}[0-9]{5}))$/;
    const englishLettersRegex = /^[a-zA-Z]+$/;
    const emailRegex1 = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    const emailRegex2: any = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    const thaiMobileNoRegex: any = /^(0)[6-9]{1}[0-9]{8}|^(999)\d{7}/;
    const otherThaiMobileNoRegex: any = /^(0)[2-9]{1}[0-9]{7,8}/;
    const actionRegex = /^(?:e|w|s|n|se|sw|ne|nw|all|crop|move|zoom)$/;
    const dataUrlRegex = /^data:/;
    const jpegDataUrlRegex = /^data:image\/jpeg;base64,/;
    const tagNameRegex = /^(?:img|canvas)$/i;
    const cssSuffixRegex = /^(?:width|height|left|top|marginLeft|marginTop)$/;
    const urlOriginRegex = /^(https?:)\/\/([^:/?#]+):?(\d*)/i;
    const dataUrlHeadRegex = /^data:.*,/;

    function isPassport(InputValue: string) {
    const regex =
      /^((?!.*(012345|123456|234567|345678|456789|000000|111111|222222|333333|444444|555555|666666|777777|888888|999999))[0-9a-zA-Z]{6,9})$/;

    return (
      regex.test(InputValue) &&
      !this.isPhone(InputValue) &&
      !this.isForeign(InputValue)
    );
  }

    function isPhone(InputValue: string) {
    const regex = /^0\d{9}$/;
    return regex.test(InputValue);
  }

  function isForeign(InputValue: string) {
    const regex = /^[0|6|7]{1}[0-9]{12}$/;
    return regex.test(InputValue);
  }

  function isIdCard(InputValue: string) {
    const regex = /^[1-5|8-9]{1}[0-9]{12}$/;
    return regex.test(InputValue);
  }

    function containsThaiAndSpecialCharacters(InputValue: string) {
    const regex = /[\u0E00-\u0E7F\s\p{P}\p{S}]/u;
    return regex.test(InputValue);
  }