Skip to content

Instantly share code, notes, and snippets.

@amirshnll
Last active December 12, 2023 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirshnll/503245c77a5077d1b4a7d0855f80a7da to your computer and use it in GitHub Desktop.
Save amirshnll/503245c77a5077d1b4a7d0855f80a7da to your computer and use it in GitHub Desktop.
My Regular Expression - REGEX

My Regular Expression - REGEX

Email Address Validation:

/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/

URL Validation:

/^(https?|ftp):\/\/[^\s\/$.?#].[^\s]*$/i";

Username Validation (alphanumeric and underscores):

/^[a-zA-Z0-9_]+$/

Password Validation (minimum 8 characters, at least one uppercase, one lowercase, one number, and one special character):

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/

Date (YYYY-MM-DD) Validation:

/^\d{4}-\d{2}-\d{2}$/

HTML Tags Removal:

/<[^>]+>/

Numeric-only String:

/^[0-9]+$/

Alphabetic-only String:

/^[a-zA-Z]+$/

Alphanumeric String:

/^[a-zA-Z0-9]+$/

Hex Color Code:

/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/

Credit Card Number (Simple):

/^\d{4}-\d{4}-\d{4}-\d{4}$/

IPv4 Address:

/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/

Phone Number (US):

/^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/

Phone Number (IR):

/^(\\+98|0)?9\\d{9}$/

Mobile Number (IR):

/^09(1[0-9]|3[1-9])-?[0-9]{3}-?[0-9]{4}$/

Whitespace Removal:

/\s+/

Positive Integer:

/^[1-9]\d*$/

Negative Integer:

/^-[1-9]\d*$/

HTML Attribute Extraction:

/([a-zA-Z-]+)="([^"]*)"/

Time in HH:MM format:

/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/

ZIP Code (US):

/^\d{5}(-\d{4})?$/

ZIP Code (IR):

/^[0-9]{11}$/

Floating Point Number:

/^\d+(\.\d+)?$/

Alphanumeric with Underscores:

/^[a-zA-Z0-9_]+$/

Iranian national ID:

/^[0-9]{10}$/

UUID (Universally Unique Identifier):

/^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[1-5][a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$/

YouTube Video ID:

/^[a-zA-Z0-9_-]{11}$/

Markdown Image Tag Extraction:

/!\[([^\]]*)\]\(([^)]+)\)/

JWT (JSON Web Token) Extraction:

/^([a-zA-Z0-9-_=]+)\.([a-zA-Z0-9-_=]+)\.([a-zA-Z0-9-_.+/=]+)$/

MIME Type Extraction from File Extension:

/\.([a-zA-Z0-9]+)$/

Markdown Headings:

/^#{1,6}\s(.+)$/

IPv6 Address:

/^([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}$/

CSS Class Extraction:

/\.[a-zA-Z0-9_-]+/

Positive or Negative Decimal with optional exponent:

/^-?\d+(\.\d+)?([eE][+-]?\d+)?$/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment