Bỏ qua đến nội dung chính
FreeOnlineTools Go
Tiếng Việt
how-to

Cách Kiểm tra Biểu thức Chính quy Từng Bước Một

By FreeOnlineTools Team · Updated 2026-09-02

Quick Answer

Để test regex: mở Regex Tester, nhập pattern vào trường regex, nhập văn bản test và match được highlight ngay. Công cụ hiển thị vị trí match, capture group và hỗ trợ flag (g, i, m, s, u). Tất cả xử lý xảy ra cục bộ.

Introduction

Regular expression (regex) là pattern dùng để match, search hoặc replace văn bản. Regex mạnh nhưng notor khó đọc và debug —một ký tự sai vị trí có thể đổi ý nghĩa hoàn toàn. Regex tester cho phép thử nghiệm pattern với văn bản mẫu, thấy match highlight và inspect capture group.

Step by Step

  1. Open the Regex Tester tool

    Go to the Regex Tester tool page. The tool has fields for the regex pattern, flags, and test text.

  2. Enter your regex pattern

    Type your regex pattern (without surrounding slashes). For example, \d+ matches one or more digits, [A-Z]\w+ matches a capitalized word. The tool parses the pattern as you type.

  3. Set the flags

    Choose flags: g (global — find all matches), i (case-insensitive), m (multiline — ^ and $ match line boundaries), s (dotall — . matches newlines), u (unicode). Most tests use g and sometimes i.

  4. Enter your test text

    Paste or type the text you want to test against. The tool applies the regex and highlights all matches in the text instantly.

  5. Review matches and capture groups

    The tool shows each match with its position and content. If your pattern has capture groups (parentheses), each group's content is displayed. Use this to debug why a pattern matches (or doesn't match) as expected.

Examples

Match all email-like patterns

Input: Pattern: [a-z]+@[a-z]+\.[a-z]+, Flags: gi, Text: 'Contact alice@example.com or Bob@TEST.ORG'

Output: Matches: 'alice@example.com', 'Bob@TEST.ORG'

Extract numbers with capture groups

Input: Pattern: (\d+)-(\d+), Flags: g, Text: 'Order 123-456 and 789-012'

Output: Match 1: '123-456' (group 1: '123', group 2: '456') Match 2: '789-012' (group 1: '789', group 2: '012')

Validate a date format

Input: Pattern: ^\d{4}-\d{2}-\d{2}$, Flags: none, Text: '2026-09-02'

Output: Match: '2026-09-02' (full string match)

Common Problems

  • Forgetting to escape special characters —characters like ., *, +, ?, ^, $, (, ), [, ], {, }, |, \ are special. Use \ to escape them literally.
  • Greedy vs lazy matching —* and + are greedy (match as much as possible). Add ? for lazy matching (e.g., .*? matches as little as possible).
  • Catastrophic backtracking (ReDoS) —some patterns with nested quantifiers can take exponential time on certain inputs. Test with realistic inputs and watch for timeouts.
  • Anchoring —without ^ and $, the pattern matches anywhere in the string. Use ^...$ to match the entire string.

Tips

  • Start with a simple pattern and add complexity incrementally —debugging a complex regex all at once is very hard.
  • Use named capture groups (?<name>...) for readability —group names make the intent clearer than numbered groups.
  • Test with both matching and non-matching inputs to ensure your pattern does not over-match.
  • Use our Regex Tester for instant, private testing —your patterns and text never leave your browser.

Related Tools

Related Guides

References