Regex for Beginners: Learn Regular Expressions in 10 Minutes (2026)

What is a regular expression

A Regular Expression (regex or regexp) is a powerful tool for matching strings. It uses a set of special characters to describe "what text I'm looking for".

✍️ Author:DevToolbox Team📅 Updated:2026-06-24📎 References:RFC Standards

📌 Key Takeaways

✍️ Author:DevToolbox Team📅 Updated:2026-06-24📎 References:RFC Standards

📌 Key Takeaways

Whether validating emails, extracting logs, or batch-renaming files, regex is a programmer's Swiss army knife.

10 most common regex patterns

The 10 examples below cover 80% of daily use cases.

Basic metacharacters

1. \d matches digits (equivalent to [0-9])

2. \w matches alphanumeric and underscore (equivalent to [a-zA-Z0-9_])

3. \s matches whitespace (space, Tab, newline)

4. . matches any single character (except newline)

5. ^ matches start of string

6. $ matches end of string

Quantifiers

7. * 0 or more times

8. + 1 or more times

9. ? 0 or 1 time

10. {n,m} n to m times

Real-world examples

Email: ^[\w.+-]+@[\w-]+\.[\w.-]+$

Phone (US): ^\+?1?[2-9]\d{2}[2-9]\d{2}\d{4}$

URL: ^https?://[\w.-]+(/[\w./?=&%-]*)?$

IPv4: ^(\d{1,3}\.){3}\d{1,3}$

Extract numbers: \d+

Date (YYYY-MM-DD): ^\d{4}-\d{2}-\d{2}$

Hex color: ^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$

Trim whitespace: \s+ replace with single space

DevToolbox online tester

The fastest way to learn regex is to test as you go. DevToolbox offers a real-time regex tester with highlighted matches:

https://devstoolbox.net/en/tools/regex-tester.html

Supports all JavaScript flags: g (global), i (case-insensitive), m (multiline), s (dotAll), u (Unicode), y (sticky).

FAQ

Q1: Is regex hard to learn?

A: 10 minutes to start, 10 years to master. For daily use, basic metacharacters + quantifiers are enough.

 

Q2: Is Python regex the same as JavaScript?

A: Mostly the same syntax, with minor differences in advanced features.

 

Q3: How to debug regex?

A: Use DevToolbox regex tester with real-time highlighting.

 

Q4: What if performance is bad?

A: Avoid catastrophic backtracking (e.g., (a+)+ should be a+), use non-greedy quantifiers (*?, +?).

 

Q5: How to use named capture groups?

A: (?...) to define, \k to backreference.

Related tools

Regex Tester: https://devstoolbox.net/en/tools/regex-tester.html

JSON Formatter (often used for parsing logs): https://devstoolbox.net/en/tools/json-formatter.html

Text Deduplicator: https://devstoolbox.net/en/tools/text-dedupe.html

Article by DevToolbox. Try our free tools: https://devstoolbox.net

🔗 Share: 𝕏 📘 ✈️ 💬

FAQ: Common Questions

Q: 正则表达式是什么?

正则表达式(Regular Expression)是用特定符号描述字符串模式的工具,用于匹配、查找、替换文本。例如 `/\d+/` 匹配一个或多个数字。

Q: 正则表达式难学吗?

入门只需 30 分钟。掌握元字符(. * + ? \d \w \s)、字符集([abc])、锚点(^ $)、分组(())就够了。复杂场景需要实战积累。

Q: 贪婪匹配和非贪婪匹配有什么区别?

贪婪匹配(默认)尽可能多地匹配字符,如 `/<.*>/` 匹配 `<b>hello</b>` 整段;非贪婪匹配(在量词后加 ?)尽可能少地匹配,如 `/<.*?>/` 只匹配 `<b>` 和 `</b>`。

Q: 正则表达式和通配符有什么区别?

通配符是 shell 用的简单模式(如 `*.txt`),正则表达式是功能更强大的字符串匹配语言,两者语法和用途都不同。

🧰
Add to Home Screen
Works offline, launches instantly