Regex for Beginners: 10 Useful Patterns to Know

THEJORD Team5 min read
regexprogrammingvalidationdevelopment

Regex guide for beginners: 10 essential patterns for email, URL, numbers. Basic syntax and examples.

Regex for Beginners: 10 Useful Patterns to Know

Have you ever tried to validate an email with a series of if statements and includes()? Or extract all phone numbers from a document? After the third failed attempt, you googled "regex" and ended up on a page full of incomprehensible symbols like ^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$.

Regex looks like black magic, but it's actually an incredibly powerful tool once you understand the logic. In this guide, I'll show you the 10 most useful regex patterns you'll actually use in your daily work, with clear explanations and ready-to-copy code.

Regex in 60 seconds: the essential syntax

Before we look at the patterns, here are the fundamental symbols you need to know:

.Any character (except newline)
\dAny digit (0-9)
\wLetter, number, or underscore
\sSpace, tab, newline
+One or more of the previous
*Zero or more of the previous
?Zero or one of the previous
{n,m}From n to m repetitions
^Start of string
$End of string
[abc]One of a, b, or c
(group)Capture a group

With these 12 symbols, you can build 90% of the regex you'll ever need. Now let's see the practical patterns.

The 10 regex patterns you'll actually use

1. Email (basic validation)

const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

// Test
emailRegex.test('john@example.com');  // true
emailRegex.test('john@');             // false

How it works: one or more alphanumeric characters, then @, then domain, then . and extension of at least 2 letters.

2. URL (http/https)

const urlRegex = /^https?:\/\/[\w.-]+\.[a-zA-Z]{2,}(\/\S*)?$/;

urlRegex.test('https://example.com');           // true
urlRegex.test('http://sub.example.com/page');   // true

Note: https? means "http" optionally followed by "s".

3. US Phone number

const phoneRegex = /^(\+1)?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/;

phoneRegex.test('555-123-4567');      // true
phoneRegex.test('+1 (555) 123-4567'); // true
phoneRegex.test('5551234567');        // true

How it works: optional country code, then 3 digits (area code), then 3+4 digits with optional separators.

4. Social Security Number (US)

const ssnRegex = /^\d{3}-\d{2}-\d{4}$/;

ssnRegex.test('123-45-6789'); // true

Structure: 3 digits + hyphen + 2 digits + hyphen + 4 digits.

5. Strong password

// At least 8 characters, 1 uppercase, 1 lowercase, 1 number
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/;

passwordRegex.test('Password1');  // true
passwordRegex.test('password');   // false (no uppercase, no number)

How it works: (?=.*[a-z]) is a "lookahead" that checks for at least one lowercase letter without consuming characters.

6. Date format (mm/dd/yyyy)

const dateRegex = /^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$/;

dateRegex.test('06/15/2024'); // true
dateRegex.test('13/32/2024'); // false

Note: this regex validates format but doesn't check if the date actually exists (e.g., 02/31 would pass).

7. IP Address (IPv4)

const ipRegex = /^(\d{1,3}\.){3}\d{1,3}$/;

// More precise version (0-255)
const ipPreciseRegex = /^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/;

ipPreciseRegex.test('192.168.1.1');  // true
ipPreciseRegex.test('256.1.1.1');    // false

8. Hashtags

const hashtagRegex = /#\w+/g;

const text = "Check out #javascript and #webdev";
text.match(hashtagRegex); // ['#javascript', '#webdev']

Flag g: finds all occurrences, not just the first one.

9. Multiple spaces → single space

const text = "Too    many    spaces    here";
text.replace(/\s+/g, ' '); // "Too many spaces here"

Super useful for cleaning user input or copied text.

10. Extract numbers from a string

const text = "The price is $49.99 (20% discount)";
text.match(/\d+\.?\d*/g); // ['49.99', '20']

How it works: \d+ one or more digits, \.? optional period, \d* zero or more digits after the period.

How to test regex

Method 1: Browser console

// Create the regex
const regex = /test/;

// Test it
regex.test('this is a test'); // true

// Find matches
'email: john@test.com'.match(/\S+@\S+/); // ['john@test.com']

Method 2: Regex101 (recommended)

Go to regex101.com: write the regex, paste your test text, and see in real-time what gets matched with detailed explanations. This tool is fantastic because it explains each part of your regex in natural language, highlights captured groups, and shows exactly where and why a regex fails.

Regex101 also supports different regex "flavors" (JavaScript, Python, PHP, Go) which have small syntax differences. Make sure to select the right language before testing.

Method 3: VS Code and other editors

Almost all modern code editors support regex search. In VS Code, open search (Ctrl+F), click the .* icon to enable regex mode, and start searching. This is super useful for finding and replacing patterns across your entire project.

Common mistakes to avoid

  • Forgetting to escape special characters: to search for a literal period use \., not .
  • Regex too permissive: .+ matches everything, including what you don't want
  • Regex too strict: a "perfect" email regex has 6000+ characters and nobody actually uses it
  • Not using flags: i for case-insensitive, g for global match
  • Not testing edge cases: empty string, spaces, special characters

Conclusion

Regex isn't black magic: it's a precise tool for specific problems. With the 10 patterns in this guide, you cover 90% of real-world cases:

  • Email, URL, phone validation
  • Data extraction (numbers, hashtags)
  • Text cleanup (spaces, formatting)

The trick is to start simple, always test on regex101, and don't try to build the perfect regex on the first attempt.

Try our free Regex Tester to experiment with patterns directly in your browser.