kumpulan regex

  • mempertahankan huruf, angka dan semua karakter unicode (selain itu dihapus)

/[^\p{L}\d\s]/gu


  • menghapus semua sepasi (lebih dari satu)

/\s+/g


  • menghapus spasi di awal dan akhir string.

/^\s+|\s+$/g


  • meghapus karakter "-" di awal dan di akhir string

/^-+|-+$/g


  • memvalidasi ipv6

const isIpv6 = /^(?:(?:(?:[0-9a-fA-F]{1,4}:){6}(?:[0-9a-fA-F]{1,4}:)?[0-9a-fA-F]{1,4}|(?:(?::[0-9a-fA-F]{1,4}){0,6})?)((?::[0-9a-fA-F]{1,4}){1,2})?|(?:[0-9a-fA-F]{1,4}:){1,7}:|::|(?:[0-9a-fA-F]{1,4}:){1,6}[0-9a-fA-F]{1,4}|(?:(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2})|(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:(?::[0-9a-fA-F]{1,4}){1,6}|:(?::[0-9a-fA-F]{1,4}){1,7})$/.test(ip);


  • memvalidasi ipv4

const isIpv4 = /^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$/.test(ip);