Class of 2026

FINAL YEAR.
LOST?

Placements feel uncertain.
Career path unclear.
You need direction.

We'll help you figure it out.
Let's connect.

Real guidance. Real results.

Skip to main content

JavaScript Regular Expressions (RegEx) Cheatsheet

JavaScript uses RegExp objects to work with regular expressions for searching and manipulating strings.


๐Ÿง  Syntax Basics

const pattern = /expression/flags;
const regex = new RegExp("expression", "flags");
Flag Meaning
g Global search (all matches)
i Case-insensitive
m Multiline mode
s Dot matches newline
u Unicode
y Sticky matching

๐Ÿ” Common RegEx Patterns

1. Match an Email Address

const regex = /^[\w.-]+@[a-zA-Z\d.-]+\.[a-zA-Z]{2,}$/;
  • Explanation: Matches user@domain.com
  • \w = word char, . = dot, - = hyphen

2. Match a URL

const regex = /https?:\/\/(www\.)?[a-zA-Z0-9-]+\.[a-zA-Z]{2,}(\/\S*)?/;
  • Matches http://, https://, optional www, domain, and optional path

3. Validate a Phone Number (US)

const regex = /^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/;
  • Matches formats like (123) 456-7890, 123-456-7890, 1234567890

4. Match Only Digits

const regex = /^\d+$/;
  • Only digits (0-9), at least one

5. Match Only Letters

const regex = /^[a-zA-Z]+$/;
  • Only alphabetic characters

6. Check for Whitespace

const regex = /\s/;
  • Matches any whitespace character

7. Password Strength (8+ chars, upper, lower, number, special)

const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@#$%^&+=!]).{8,}$/;
  • Uses lookaheads to ensure character variety

8. Extract Numbers from a String

const regex = /\d+/g;
const numbers = "abc123def456".match(regex); // ['123', '456']

9. Detect HTML Tags

const regex = /<[^>]*>/g;
  • Matches any HTML tags like <div>, </p>, etc.

10. Match Dates (MM/DD/YYYY)

const regex = /^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$/;
  • Matches US-style dates like 04/16/2025

11. Match Hex Colors

const regex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
  • Matches hex colors like #fff or #12a3ef

12. Match Time (24-hour HH:MM)

const regex = /^([01]\d|2[0-3]):([0-5]\d)$/;
  • Matches times like 14:30, 09:45

๐Ÿงช Using RegEx in JavaScript

const pattern = /^\d{3}$/;
console.log(pattern.test("123")); // true
console.log("abc123".match(/\d+/g)); // ['123']

๐Ÿ“Œ Tips

  • Use RegExp.test() to check for a match (true/false)
  • Use String.match() to extract matches
  • Escape special characters like . or ? with \

Let me know if you'd like this exported to a .md file or extended with advanced patterns (e.g., IPv4, slugs, currency formats).