What Is Regex (a Regular Expression)? A Beginner's Guide with Tested Examples and Common Mistakes
Updated: November 6, 2026 · AracMerkezi
A regular expression (regex for short) is a pattern language used to search for, validate or replace a particular pattern inside text. It is very useful in form validation, log analysis, data cleaning and find-and-replace operations. In this guide we collect the basic elements in a table and then actually run the examples and show their results: capturing and reformatting a date, greedy matching, where phone and email patterns succeed and where they mislead, and the problem with Turkish letters. We tried the examples in Node.js (the JavaScript engine).
Basic elements
| Pattern | Meaning |
|---|---|
. | Any character |
\d | Digit |
\w | Letter, digit or underscore |
\s | Whitespace character |
^ and $ | Start and end of line |
[abc] | a, b or c |
[^abc] | Anything except a, b, c |
| Vertical bar character | Choice between a or b |
Quantifiers
*: zero or more+: one or more?: zero or one{3}: exactly 3 times,{2,4}: 2 to 4 times
Groups
Parentheses ( ) group a part and capture the matched portion. For example, (\d{2})\.(\d{2})\.(\d{4}) captures the day, month and year of a date separately. If you do not want capturing and only want grouping, use the (?:...) form.
Flags
Letters added to the end of the pattern change its behavior: g finds all matches (otherwise only the first), i removes case sensitivity, and m treats the start and end of each line separately. The test tool on this site adds g itself even if you do not put it in the flags field.
Example 1: Capturing and reformatting a date
Let the input be 31.10.2026 and the pattern ^(\d{2})\.(\d{2})\.(\d{4})$.
- The match covers the whole text; the captured groups are
31,10and2026, in order. - If you write
$3-$2-$1as the replacement text, the result is2026-10-31; each$nis the content of group n. - If you want to name the groups, use the
(?<day>\d{2})form; for05.11.2026, the day is read as05, the month as11and the year as2026.
We verified these results by running them in JavaScript. The test tool only lists matches; you need to do the replacement in your own code or editor.
Example 2: Greedy and lazy matching
Let the text be <b>kalın</b> ve <i>eğik</i>.
- The pattern
<.+>gives a single match: the whole text. That is because.+captures as many characters as possible (greedy). - The pattern
<.+?>gives four separate matches:<b>,</b>,<i>,</i>. The?after the quantifier makes it lazy; it captures as few characters as possible.
Example 3: Testing ready-made patterns with real inputs
The results below were obtained in Node.js.
| Pattern | Input | Result |
|---|---|---|
email ^[^\s@]+@[^\s@]+\.[^\s@]+$ | ali@site.com | Matches |
| email (same pattern) | ali@site | Does not match |
| email (same pattern) | ali @site.com | Does not match |
11 digits ^[1-9]\d{10}$ | 12345678901 | Matches |
| 11 digits (same pattern) | 01234567890 | Does not match |
date ^\d{2}\.\d{2}\.\d{4}$ | 31.10.2026 | Matches |
| date (same pattern) | 31.02.2026 | Matches |
Results for the phone pattern ^(\+90|0)?5\d{9}$:
05321234567→ matches+905321234567→ matches5321234567→ matches0532 123 45 67→ does not match
There are two points to notice. Because the phone pattern leaves the leading 0 or +90 optional, a 10-digit number without the 0 is also counted as valid; if you do not want that, tighten the pattern. Also, a number with spaces in it is rejected; you may need to clean the user's input with \s+ first.
Ready-made examples
- Simple email check:
^[^\s@]+@[^\s@]+\.[^\s@]+$ - Mobile phone (05xx format):
^(\+90|0)?5\d{9}$ - DD.MM.YYYY date format:
^\d{2}\.\d{2}\.\d{4}$ - 11-digit number (not starting with 0):
^[1-9]\d{10}$
These examples check only the format. You cannot tell with regex whether an email or an ID number really exists; a nonexistent date such as 31.02.2026 also passes the date pattern.
Turkish characters and letter case
In JavaScript, \w covers only ASCII letters, digits and the underscore. In the text Şeker, \w+ missed the first letter Ş and returned eker. To cover Turkish letters, use either an explicit set such as [A-Za-zÇĞİÖŞÜçğıöşü]+ or \p{L}+ with the u flag; both returned Şeker and çay from the text Şeker çay. Be careful with letter case too: even with the i flag, the pattern istanbul did not match İSTANBUL. For Turkish dotted and dotless i pairs, convert the text yourself first.
Common mistakes
- Writing
.instead of\.for a dot (it matches any character). The pattern^\d{2}.\d{2}$accepted the text12x34; once\.was written, it rejected it. - Forgetting greedy matching:
.*captures as many characters as possible. - Not putting the start and end anchors (
^,$). The pattern\d{3}matches insideab1234c;^\d{3}$does not. - Overly complex nested quantifiers: some patterns can run very slowly (ReDoS).
- Forgetting that the regex syntax of programming languages can differ slightly.
ReDoS: a pattern can slow down
If you give a pattern with nested quantifiers such as ^(a+)+$ a text with many a characters followed by a b, the engine tries countless paths. In our test it took about 0.1 seconds with 24 a characters and about 0.5 seconds with 26; each extra character roughly doubles the time. The same text finished instantly with the pattern ^a+$, even at 5,000 characters. Avoid nested quantifiers in patterns that work on text coming from users.
A path to follow when writing a pattern
- Collect real examples you have: those that should match and those that should not.
- Start with the simplest pattern, then narrow it step by step.
- For validation, add
^at the start and$at the end; in a search, do not put anchors. - Test both positive and negative examples; seeing it "work" is not enough.
- If it gets complicated, split the pattern into parts and explain it with a comment if needed.
Short glossary
- Anchor: An element that marks a position and consumes no characters (
^,$). - Quantifier: A mark that says how many times an element repeats (
*,+,?,{n}). - Capture group: A part marked with parentheses that can later be referred to as
$1,$2and so on. - ReDoS: A badly written pattern slowing down excessively on certain inputs.
When to use it and its limits
Regex is very suitable for format checking, text extraction and bulk replacement. It is not suitable for nested structures (an HTML tree, balanced parentheses); use a real parser for those. If a validity check has to be exact (a real email, a real date), do another validation in addition to regex.
Testing
To try your pattern on sample text, use the Regex Test Tool page. The tool uses the JavaScript regex engine and lists the first 100 matches. When working with HTML content, you may also need the HTML Entity Encode/Decode tool. To split address parts, see the URL parser tool, and for data formats, the JSON guide.
Frequently asked questions
Can regex verify that an email really exists?
No; regex only checks the format and cannot tell whether the address exists.
What does \d mean?
It matches any digit.
What is greedy matching?
It is when quantifiers such as * and + capture as many characters as possible; adding ? after them makes them lazy.
Tools mentioned in this guide
More guides
- What Are JSON, Base64 and URL Encoding? Encoding vs Encryption vs Hashing – What do JSON, Base64, URL encoding, HTML entities, SHA and JWT do? Encoding vs encryption vs hashing, with examples that were run and verified.
- What Are HTTP Status Codes? 200, 301, 404, 500 and Troubleshooting – What do HTTP status codes mean? Codes 200, 301, 404, 500, testing with curl, the cost of redirect chains, soft 404s and site-migration examples.
- What Is chmod? Linux File Permissions 755, 644 and 777 Explained: Reading ls -l, umask and Examples – What is chmod? Read ls -l output, understand 755 644 600 777, symbolic mode, umask and special bits. Commands were run and verified.
- What Are an IP Address, CIDR and a Subnet Mask? How Many Addresses Is /24? A Calculation Method – How do you calculate CIDR notation and subnet masks? The block-size method, network and broadcast addresses, splitting into subnets and private ranges.
- What Is curl? Basic Flags and Converting a curl Command to fetch – What is curl and what do the -X, -H and -d flags do? A POST request example, the JavaScript fetch equivalent of a curl command, and debugging tips.
This article is for general information only and is not legal, financial or medical advice. We are not an official institution.