na.to.
📚 All keywords › 🧰 Free Web Tools › Regular Expression Basics: Common Patterns, Greedy vs Lazy
KO EN
🔍

Regular Expression Basics: Common Patterns, Greedy vs Lazy

Core regex symbols, classes, quantifiers and groups, greedy vs lazy matching, the g, i, m and s flags, and pitfalls with Korean text, with examples.

📚 Free Web Tools · 18/52· ⏱ About 4min read ·Information updated 2026-11-03
📋 Key facts5
Flavor
This guide follows JavaScript (browser) syntax
Quantifiers
* zero or more, + one or more, ? zero or one
Lazy matching
Add ? after a quantifier (*?, +?)
Flags
g all, i ignore case, m multiline, s dot matches newline
Hangul
\w does not match Hangul; use [가-힣]

A way to describe text patterns

A regular expression describes the shape of text, such as three digits followed by a hyphen, in symbols, so you can find and replace it. Regular expressions are used in programming languages, text editors, spreadsheets and more, but the details differ from engine to engine. This guide follows JavaScript (browser) syntax, which is what this site's regex tester uses. The tester shows matches, the match count and groups as soon as you type a pattern.

Basic symbols

Most characters match themselves, but some symbols have special meanings. A dot (.) matches any single character except a line break, ^ means the start and $ the end. Square brackets match one of the characters inside: [abc] is a, b or c, [0-9] is one digit and [^0-9] is one character that is not a digit. To match a special symbol literally, put a backslash in front, as in \.

  • \d digit, \w letter, digit or underscore, \s whitespace
  • \D, \W and \S are the opposites
  • . any character except a line break; \. is a literal dot
  • ^ start, $ end, \b word boundary

Repetition and groups

A quantifier applies to the character or group right before it. * means zero or more times, + one or more, ? zero or one, {3} exactly three and {2,4} two to four. Parentheses ( ) group several characters so they repeat as a unit, and the matched part is saved as a group that you can reuse in a replacement as $1, $2 and so on. Use (?: ) when you do not need it saved and (?<name> ) to give it a name. A vertical bar | means or.

Greedy and lazy matching

Quantifiers are greedy by default, matching as much as possible. In a sentence containing <b>bold</b> and <i>italic</i>, the pattern <.*> matches everything from the first < to the last > as one piece. Adding ? after the quantifier, as in <.*?>, makes it lazy so it matches as little as possible, catching one tag at a time. Nested structures such as HTML are hard to handle exactly with regular expressions alone, so it is safer to use them only for simple cleanup.

Flags

Flags change how the whole pattern behaves. g finds every match instead of only the first, i ignores case, m makes ^ and $ also match at the start and end of each line, and s lets the dot match line breaks. This site's tester lets you switch these four flags on and off. When checking a multi-line list line by line it is easy to forget m, so if you get only one match, check the flags first.

Common pattern examples

The examples below are for rough format checks. Formats with complex rules, such as email addresses, are hard to validate perfectly with one regular expression, and no regular expression can tell you whether an address actually exists. When validating input, anchor the pattern with ^ and $ so that a partial match does not pass.

  • Digits only: ^\d+$
  • Korean mobile number with hyphens: ^01[0-9]-\d{3,4}-\d{4}$
  • Rough email check: ^[^\s@]+@[^\s@]+\.[^\s@]+$
  • Collapse runs of spaces: replace \s+ with a single space
  • Hangul only: ^[가-힣]+$

Korean text and slow patterns

In JavaScript, \w means only Latin letters, digits and underscore, so it does not match Hangul. Use [가-힣] for Hangul syllables, and add separate ranges if you also need standalone consonants and vowels (ㄱ, ㅏ). Nesting a repetition inside a repetition, as in (a+)+, can cause catastrophic backtracking, where matching time explodes as the input gets longer. This site's tester stops and warns you if matching does not finish within one second; if you see that warning, rewrite the pattern so repetitions are not nested.

Summary

The fastest way to learn regular expressions is to test them one at a time on small samples and build up.

  • Put \ before a special symbol to match it literally
  • Use *? or +? to match as little as possible
  • Turn on m to check line by line
  • Use [가-힣] instead of \w for Hangul
  • Anchor input checks with ^ and $

🔗 More in this category

🧰 Related tools

🌍 Search the web for this

Each button runs this keyword on that search engine