Irreva logo
Explore Irreva
DeveloperJanuary 13, 2026· 7 min read· Updated June 10, 2026

What Is Regex and How to Use It — A Beginner's Guide

Hasanur Rahman

Written by Hasanur Rahman

Founder & Full-Stack Developer · Irreva · Rangpur, Bangladesh

Regular expressions — usually shortened to regex or regexp — are patterns used to search, match, and manipulate text. They look intimidating at first: a string like ^[\w.-]+@[\w-]+\.[a-z]{2,}$ can seem like noise. But once you understand the building blocks, regex becomes one of the most powerful tools in any developer's toolkit. This guide explains how it works from scratch.

The core building blocks of regex

A regex pattern is made up of literals and metacharacters. Literals match themselves — the pattern cat matches the text 'cat' exactly. Metacharacters have special meaning and let you build flexible patterns.

The dot (.) matches any single character except a newline. The asterisk (*) means 'zero or more of the previous item'. The plus (+) means 'one or more'. The question mark (?) means 'zero or one' — making the previous item optional. These quantifiers are the first thing to learn.

Character classes let you match a set of characters. [abc] matches any one of a, b, or c. [a-z] matches any lowercase letter. [0-9] matches any digit. Negated classes like [^abc] match anything except those characters.

  • . — any single character
  • * — zero or more of the previous item
  • + — one or more of the previous item
  • ? — zero or one (makes previous item optional)
  • [abc] — character class (matches a, b, or c)
  • ^ — start of string (or negation inside [])
  • $ — end of string
  • \d — any digit (shorthand for [0-9])
  • \w — any word character (letters, digits, underscore)
  • \s — any whitespace character

Anchors, groups, and alternation

Anchors don't match characters — they match positions. ^ matches the start of a string and $ matches the end. So ^hello matches 'hello world' but not 'say hello'. Using both — ^hello$ — only matches the string 'hello' with nothing else.

Parentheses create groups: (abc)+ matches 'abc', 'abcabc', 'abcabcabc', and so on. Groups also capture the matched text, which you can reference in replacements or extract in code.

The pipe | means 'or'. cat|dog matches 'cat' or 'dog'. Combined with groups: (cat|dog)s matches 'cats' or 'dogs'.

Practical regex patterns you'll actually use

Regex is most useful for validation and text extraction. Here are patterns that come up constantly in real projects.

Email validation is a classic use case. A simple pattern like ^[\w.-]+@[\w-]+\.[a-z]{2,}$ catches most valid emails without being overly strict. Perfect email validation via regex is famously complex — for most purposes a reasonable approximation is fine.

For extracting data from text — log files, HTML, structured strings — regex combined with capture groups lets you pull out specific fields. A pattern like (\d{4})-(\d{2})-(\d{2}) extracts year, month, and day from a date string like '2026-01-10'.

  • Email: ^[\w.-]+@[\w-]+\.[a-z]{2,}$
  • URL: https?:\/\/[\w-]+(\.[\w-]+)+(\/[\S]*)?
  • US phone: ^\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})$
  • IPv4: ^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$
  • Date YYYY-MM-DD: ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

How to test regex online

Writing regex without a live tester is painful. An online regex tester shows you which parts of your test string match as you type, highlights capture groups, and tells you immediately when a pattern change breaks something.

The Irreva Regex Tester supports JavaScript regex syntax, lets you set flags (global, case-insensitive, multiline), and shows all matches highlighted in your input text. You can also see capture groups extracted separately.

When writing a new pattern, start with a handful of test strings that should match and a few that shouldn't. Build the pattern piece by piece and verify each component before combining them.

Frequently Asked Questions

Are regex patterns the same across all programming languages?

Mostly, but not entirely. The core syntax is similar across JavaScript, Python, Java, and most languages. However, some advanced features — like lookbehind assertions, named groups, and Unicode support — vary. Always test your pattern in the language you're actually using.

What does the 'g' flag do in JavaScript regex?

The global flag (g) makes the pattern match all occurrences in the string, not just the first one. Without it, methods like match() and replace() only operate on the first match.

How do I match a literal dot in regex?

A plain dot (.) in regex matches any character. To match a literal period, escape it with a backslash: \. — so the pattern end\.txt matches 'end.txt' but not 'endXtxt'.

What is a greedy vs lazy quantifier?

By default, quantifiers like * and + are greedy — they match as many characters as possible. Adding ? makes them lazy: they match as few characters as possible. For example, <.*> matches an entire HTML tag including everything inside it, while <.*?> matches just the first tag.

Can regex be used for HTML parsing?

For simple, predictable cases — extracting a specific attribute from a known tag structure — regex works fine. For general HTML parsing where the structure is complex or unknown, use a proper HTML parser. HTML is not a regular language and general-purpose regex parsing breaks on nested or malformed markup.

Hasanur Rahman

About the author

Hasanur Rahman

Founder & Full-Stack Developer · Irreva · Rangpur, Bangladesh

Hasanur Rahman is the founder of Irreva and a full-stack developer based in Rangpur, Bangladesh. He builds all of Irreva's tools with a focus on privacy-first, browser-based processing.