Regular Expressions for Beginners: A Practical Guide
Regular expressions are one of the most powerful tools in programming. This beginner-friendly guide explains the syntax and shows practical examples you can use right away.
Regular expressions (regex) are patterns used to match, search and manipulate text. They are available in virtually every programming language and text editor. Once you learn the basics, they become an incredibly powerful tool for validating input, parsing data, and transforming text in seconds.
Basic Syntax: Literals and Wildcards
The simplest regex is a literal match — /hello/ matches the word "hello". The dot (.) is a wildcard that matches any single character except a newline. So /h.llo/ matches "hello", "hallo", "hxllo" and so on. To match a literal dot, you need to escape it with a backslash: /hello\./ matches "hello." with the period.
Quantifiers: How Many Times?
Quantifiers control how many times a pattern can repeat. The asterisk (*) means "zero or more", plus (+) means "one or more", and the question mark (?) means "zero or one". Curly braces specify exact counts: {3} means exactly 3, {2,5} means between 2 and 5. So /\d{3}-\d{4}/ would match a phone number format like "555-1234".
Character Classes and Shortcuts
Square brackets define character classes. [aeiou] matches any vowel; [a-z] matches any lowercase letter; [0-9] matches any digit. Shorthand classes make patterns more readable: \d matches any digit (same as [0-9]), \w matches word characters (letters, digits, underscore), and \s matches whitespace. Uppercase versions (\D, \W, \S) match the opposite.
Anchors, Groups and Alternation
The caret (^) anchors the match to the start of a string; the dollar ($) anchors it to the end. So /^\d+$/ matches a string of only digits, nothing else. Parentheses create capturing groups: /(\d{4})-(\d{2})-(\d{2})/ matches a date like "2026-06-05" and captures year, month and day separately. The pipe | means "or": /cat|dog/ matches either "cat" or "dog".
How to Use the Regex Tester
Enter your pattern in the regex field, then type your test string below. The tool highlights all matches in real-time and shows match details including captured groups. You can add flags: g (global — find all matches), i (case-insensitive), and m (multiline — ^ and $ match line starts/ends). Use it to test your patterns before putting them in code.