“\d+”, “^[a-z]+@[a-z]+.[a-z]+$” — if you have ever looked at strings of symbols like these in code or config files and thought “regex looks impossible,” you are not alone. Regular expressions become a huge productivity boost once learned, but without knowing what the symbols mean, both reading and writing them is painful.

In this guide, we explain regular expressions from basic symbols to practical patterns and testing, in plain terms for beginners. By the end, you will be able to read common patterns and write simple ones yourself.

What is a regular expression?

A regular expression (regex) is a notation for describing string patterns. It is used for searching, replacing and validating text - checking whether an input looks like an email address, extracting all dates from a document, and so on.

Regex works in programming languages, text editor search, and even spreadsheet functions, so it is a useful skill beyond development.

Basic symbols

Start with the symbols that appear most often:

Symbol Meaning Example Matches
\d A digit \d 5
\w Letter, digit or underscore \w A, b, 9, _
\s Whitespace (space, tab, newline) \s “ “
. Any single character . a, 5, あ
+ One or more of the previous a+ a, aa, aaa
* Zero or more of the previous a* (empty), a, aa
? Zero or one of the previous colou?r color, colour
^ Start of line ^abc abc… (start only)
$ End of line abc$ …abc (end only)
[ ] Any one character inside the brackets [abc] a, b, c
{n} Exactly n repetitions \d{3} 123
` ` Or `cat

Watch out for escaping: to match a special symbol itself, prefix it with \. A literal period is \. and a literal plus is \+.

Common practical patterns

Combining the basic symbols produces useful patterns:

Purpose Pattern Notes
Three digits \d{3} 123, 456
Postal code (123-4567) \d{3}-\d{4} 123-4567
Phone number \d{2,4}-\d{4}-\d{4} 090-1234-5678
Email (simple) ^\S+@\S+\.\S+$ [email protected]
Alphanumeric only ^[a-zA-Z0-9]+$ abc123
Hiragana only ^[ぁ-ん]+$ Japanese kana

Heads up: fully validating an email address per the RFC spec is extremely complex - the example above is a simple pattern. For real validation, rely on the type="email" input attribute or a dedicated library.

Flags change the behavior

Regex has “flags” that change how matching works. The main ones:

Flag Name Effect
g global Find all matches (otherwise only the first)
i ignore case Case-insensitive matching
m multiline ^ and $ match line starts/ends in multi-line text
s dotall . also matches newlines
u unicode Unicode mode (accurate handling of non-Latin text)

For example, to find every “hello” regardless of case, write /hello/gi.

Testing with the Tools Hub regex tester

To check whether a regex works, the Tools Hub regex tester is handy. Enter a pattern and test text, and matches are highlighted with a list of all matches.

How to use it (three steps)

1

Enter the pattern and flags

Type your regex and specify the flags you need (g, i, m, s, u).

2

Enter test text

Type the string you want to match against. Results appear in real time as you type.

3

Check the matches

Matched portions are highlighted and listed. If the pattern has an error, an error message is shown too.

The tool from this guide

Regex Tester

Free, no sign-up required, and your input never leaves your browser.

Try it now

Summary

  • Regex is a notation for describing string patterns using symbols
  • The basics are \d (digit), \w (word character), . (any character), + (one or more), * (zero or more)
  • Escape special symbols with \ to match them literally
  • Flags like g, i and m change matching behavior
  • Full email validation is complex; combine a simple pattern with standard features
  • Test your regex with a tool before using it

Trying to memorize everything at once is overwhelming. Start with just \d and + - that alone is enough to be useful in practice. Test your patterns with a tool as you go, and gradually take on more complex ones.

FAQ

What is a regular expression used for?

Searching, replacing and validating text. Form input validation, extracting data from logs, bulk text replacement - it is useful from development to everyday tasks.

Is there a difference between “\d” and “[0-9]”?

They mean the same thing. \d is shorthand for a single digit, while [0-9] means “one character from 0 to 9.” Both produce identical results.

What should I do when I get a pattern error?

Your regex has a syntax error. Check that parentheses ( ) are balanced and that special characters are properly escaped with \. The error message includes a hint about the cause.

Is there a way to test a regex?

The Tools Hub regex tester highlights matched portions as soon as you enter a pattern and test text. Real-time results make it convenient for debugging patterns.

References