# Regex Cheat Sheet: Syntax, Flags and Ready-to-Use Patterns

> Regular expression basics in one page: character classes, quantifiers, groups and flags, plus tested patterns for email, URL, date, time, IPv4, hex colours, passwords and common find-and-replace jobs.

Source: https://widgetpantry.com/en/regex-cheat-sheet · Türkçe: https://widgetpantry.com/tr/regex-ornekleri

Published 2026-11-21

A regular expression (regex) is a short language for finding, validating or replacing patterns in text. Below is the syntax you'll use most, and ready-made patterns that have all been tested. To try one on your own text, the [Regex Tester](https://widgetpantry.com/en/regex-tester) highlights matches, lists groups in a table and previews replacements.

## Core syntax

Most regular expressions are built from these pieces:

| Token | Meaning |
| --- | --- |
| . | Any character except a line break |
| \d  \w  \s | Digit, word character (letter, digit, underscore), whitespace |
| \D  \W  \S | Not a digit, not a word character, not whitespace |
| [abc]  [^abc]  [a-z] | One character in the set, not in the set, in the range |
| ^  $ | Start and end of the text (of each line with the m flag) |
| \b | Word boundary |
| *  +  ? | Zero or more, one or more, zero or one |
| {3}  {2,5}  {2,} | Exactly 3, between 2 and 5, at least 2 |
| (…)  (?:…)  (?<name>…) | Capturing group, non-capturing group, named group |
| a\|b | a or b |
| (?=…)  (?!…) | Lookahead: must follow, must not follow |
| \.  \(  \\ | The special character itself (escaped) |

## Flags

Flags change how the pattern is applied:

| Flag | Meaning |
| --- | --- |
| g | Find all matches, not just the first |
| i | Ignore case |
| m | ^ and $ match at the start/end of every line |
| s | Let . match line breaks too |
| u | Unicode: letter classes like \p{L} and emoji work correctly |

## Validation patterns

These check a whole field, so they start with ^ and end with $. To search inside longer text instead, drop the ^ and $ and use the g flag.

```
Email (practical check)
^[^\s@]+@[^\s@]+\.[^\s@]+$

URL (http / https)
^https?:\/\/[^\s/$.?#][^\s]*$

Date, YYYY-MM-DD
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

Time, 24-hour HH:MM
^([01]\d|2[0-3]):[0-5]\d$

IPv4 address
^((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)$

Hex colour (#fff or #1a2b3c)
^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$

URL slug (lowercase-with-hyphens)
^[a-z0-9]+(?:-[a-z0-9]+)*$

Password: 8+ chars, lower, upper and a digit
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
```

## Turkey-specific patterns

Removing spaces before checking phone numbers and IBANs keeps the patterns simple. The IBAN pattern checks the format only; a regex cannot verify the check digits.

```
Turkish mobile number (spaces removed): 05321234567, +905321234567
^(\+90|0)?5\d{9}$

Turkish IBAN (26 characters, no spaces)
^TR\d{24}$

Turkish postal code
^\d{5}$

A word with Turkish letters (needs the u flag)
^\p{L}+$
```

## Find-and-replace recipes

In the replacement, $1, $2 … insert captured groups and $<name> inserts a named group. You can paste these straight into the Regex Tester's replace box.

```
Collapse repeated spaces
  find: \s{2,}    replace: " " (one space)

Remove a doubled word ("the the")
  find: \b(\w+)\s+\1\b    flags: gi    replace: $1

2026-11-21 → 21/11/2026
  find: (\d{4})-(\d{2})-(\d{2})    replace: $3/$2/$1

Same with named groups
  find: (?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})    replace: $<d>/$<m>/$<y>
```

## Non-English letters

In JavaScript, \w and \b only know the English alphabet: in "café" \w+ stops at the é. To match letters in any language, use \p{L} with the u flag; \p{L}+ matches "café", "çiçek" and "straße" as whole words.

## Common mistakes

If a pattern doesn't behave, check these first:

- Unescaped dots: in example.com the dot matches any character; write \. for a literal dot.
- Greedy matching: <.+> grabs everything from the first < to the last > on a line. Use <.+?> for the shortest match.
- Missing anchors: \d{5} also matches inside "123456"; use ^\d{5}$ for exactly five digits.
- Trying to validate email perfectly: a fully standards-compliant pattern is enormous and still can't tell you the address exists. A simple check is enough; a confirmation email does the real validation.
- Parsing HTML with regex: nested tags need an HTML parser.

## Frequently asked questions

### Is regex the same in every programming language?

The core syntax (character classes, groups, quantifiers) is largely the same in JavaScript, Python, Java, C#, PHP and Go. Differences show up in advanced features: Go supports neither lookahead nor lookbehind, for example, and named group syntax varies between languages.

### How do I make a regex case-insensitive?

Add the i flag: /pattern/i in JavaScript, re.IGNORECASE in Python.

### What is the difference between matching the whole text and searching in it?

^ and $ anchor the pattern to the start and end of the text, which is what you want for validation. Without them the pattern can match anywhere, which is what you want for search and replace.

### Is my text sent to a server when I test a regex?

Widget Pantry's Regex Tester runs entirely in your browser; the pattern and text never leave your device.

## Use the tool

- [Regex Tester](https://widgetpantry.com/en/regex-tester): Online regex tester: try a regular expression on your text, see highlighted matches, capture groups and positions, and preview replacements with $1. Free, runs in your browser.
