TR

Home / Guides / What Is Regex (a Regular Expression)? A Beginner's Guide with Tested Examples and Common Mistakes

What Is Regex (a Regular Expression)? A Beginner's Guide with Tested Examples and Common Mistakes

Updated: November 6, 2026 · AracMerkezi

▶ Open Regex Tester

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

PatternMeaning
.Any character
\dDigit
\wLetter, digit or underscore
\sWhitespace character
^ and $Start and end of line
[abc]a, b or c
[^abc]Anything except a, b, c
Vertical bar characterChoice between a or b

Quantifiers

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})$.

  1. The match covers the whole text; the captured groups are 31, 10 and 2026, in order.
  2. If you write $3-$2-$1 as the replacement text, the result is 2026-10-31; each $n is the content of group n.
  3. If you want to name the groups, use the (?<day>\d{2}) form; for 05.11.2026, the day is read as 05, the month as 11 and the year as 2026.

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>.

Example 3: Testing ready-made patterns with real inputs

The results below were obtained in Node.js.

PatternInputResult
email ^[^\s@]+@[^\s@]+\.[^\s@]+$ali@site.comMatches
email (same pattern)ali@siteDoes not match
email (same pattern)ali @site.comDoes not match
11 digits ^[1-9]\d{10}$12345678901Matches
11 digits (same pattern)01234567890Does not match
date ^\d{2}\.\d{2}\.\d{4}$31.10.2026Matches
date (same pattern)31.02.2026Matches

Results for the phone pattern ^(\+90|0)?5\d{9}$:

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

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

  1. Writing . instead of \. for a dot (it matches any character). The pattern ^\d{2}.\d{2}$ accepted the text 12x34; once \. was written, it rejected it.
  2. Forgetting greedy matching: .* captures as many characters as possible.
  3. Not putting the start and end anchors (^, $). The pattern \d{3} matches inside ab1234c; ^\d{3}$ does not.
  4. Overly complex nested quantifiers: some patterns can run very slowly (ReDoS).
  5. 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

  1. Collect real examples you have: those that should match and those that should not.
  2. Start with the simplest pattern, then narrow it step by step.
  3. For validation, add ^ at the start and $ at the end; in a search, do not put anchors.
  4. Test both positive and negative examples; seeing it "work" is not enough.
  5. If it gets complicated, split the pattern into parts and explain it with a comment if needed.

Short glossary

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

This article is for general information only and is not legal, financial or medical advice. We are not an official institution.