Skip to content

Instantly share code, notes, and snippets.

@shobhitsharma
Last active July 15, 2017 03:58
Show Gist options
  • Save shobhitsharma/d55f8ba4952b2e3c728bc13115071514 to your computer and use it in GitHub Desktop.
Save shobhitsharma/d55f8ba4952b2e3c728bc13115071514 to your computer and use it in GitHub Desktop.
// Will match dates with dashes, slashes or with spaces dd-mm-yyyy dd/mm/yyyy dd mm yyyy
/^(0?[1-9]|[12][0-9]|3[01])([ \/\-])(0?[1-9]|1[012])\2([0-9][0-9][0-9][0-9])$/
// Match times in 24 hour format
/([01]?[0-9]|2[0-3]):[0-5][0-9]/
// Will match a valid date and times in the ISO-8601 format, excludes durations.
/^(?![+-]?\d{4,5}-?(?:\d{2}|W\d{2})T)(?:|(\d{4}|[+-]\d{5})-?(?:|(0\d|1[0-2])(?:|-?([0-2]\d|3[0-1]))|([0-2]\d{2}|3[0-5]\d|36[0-6])|W([0-4]\d|5[0-3])(?:|-?([1-7])))(?:(?!\d)|T(?=\d)))(?:|([01]\d|2[0-4])(?:|:?([0-5]\d)(?:|:?([0-5]\d)(?:|\.(\d{3})))(?:|[zZ]|([+-](?:[01]\d|2[0-4]))(?:|:?([0-5]\d)))))$/
// Match opening and closing HTML tags with content between
/^<([a-z1-6]+)([^<]+)*(?:>(.*)<\/\1>| *\/>)$/
// A string between 3 and 16 characters, allowing alphanumeric characters and hyphens and underscores
/^[a-zA-Z0-9_-]{3,16}$/
// RGB hex colors
/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/
// Match valid URL slugs
/^[a-z0-9-]+$/
// Verify that there is an @ symbol with something before it
/^.+@.+$/
// Match the src attribute of an HTML image tag
/^<\s*img[^>]+src\s*=\s*(["'])(.*?)\1[^>]*>$/
HTML tags
/^<([a-z1-6]+)([^<]+)*(?:>(.*)<\/\1>| *\/>)$/
Test string
URL Slug
/^[a-z0-9-]+$/
Test string
URL
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
Test string
Positive Integer
/^\d+$/
Test string
Positive number
/^\d*\.?\d+$/
Test string
Phone number
/^\+?[\d\s]{3,}$/
Test string
ID of Youtube Channel
/https?:\/\/(www\.)?youtube.com\/channel\/UC([-_a-z0-9]{22})/i
Test string
U.S./Canadian ZIP/Postal Code
/(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)/
Test string
Time in 24-hour format
/([01]?[0-9]|2[0-3]):[0-5][0-9]/
Test string
Username
/^[a-zA-Z0-9_-]{3,16}$/
Test string
Email
/^.+@.+$/
Test string
IPv4 Address
/^(?:(?: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]?)$/
Test string
Negative Integer
/^-\d+$/
Test string
Negative number
/^-\d*\.?\d+$/
Test string
New line
/[\r\n]|$/
Test string
CSS comment
/\/\*[^*]*\*+([^\/*][^*]*\*+)*\//
Test string
Morse Code
/^[.-]{1,5}(?:[ \t]+[.-]{1,5})*(?:[ \t]+[.-]{1,5}(?:[ \t]+[.-]{1,5})*)*$/
Test string
Date and time in ISO-8601 format
/^(?![+-]?\d{4,5}-?(?:\d{2}|W\d{2})T)(?:|(\d{4}|[+-]\d{5})-?(?:|(0\d|1[0-2])(?:|-?([0-2]\d|3[0-1]))|([0-2]\d{2}|3[0-5]\d|36[0-6])|W([0-4]\d|5[0-3])(?:|-?([1-7])))(?:(?!\d)|T(?=\d)))(?:|([01]\d|2[0-4])(?:|:?([0-5]\d)(?:|:?([0-5]\d)(?:|\.(\d{3})))(?:|[zZ]|([+-](?:[01]\d|2[0-4]))(?:|:?([0-5]\d)))))$/
Test string
Hex Color Value
/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/
Test string
SRC of image tag
/^<\s*img[^>]+src\s*=\s*(["'])(.*?)\1[^>]*>$/
Test string
IPv6 Address
/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([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}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/
Test string
Integer
/^-?\d+$/
Test string
Positive or negative number
/^-?\d*\.?\d+$/
Test string
ID of Youtube video
/https?:\/\/(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/watch(?:\?|#\!)v=)([\w-]{11}).*/gi
Test string
Wordpress shortcodes
/^\[([a-z-_0-9]+)([^\[]+)*(?:\](.*)\[\/\1\]|\s+\/\])$/
Test string
Image shortcode
/\[img\](.*?)\[\/img\]/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment