Regex (Regular Expressions) is one of those topics that gives many developers headaches.
But hereβs the truth: Regex is NOT difficult β you just need to understand it in the right way.
In this blog, weβll simplify regex in everyday language without frying your brain! π
β What is Regex?
Regex is a pattern-matching tool.
It is used to find, replace, validate, or extract specific patterns in a string.
Common uses:
- Finding numbers
- Validating emails
- Filtering alphabets
- Removing special characters
- Extracting dates, phone numbers, IDs, etc.
π― 1. Basic Structure of Regex
In JavaScript, a regex is written between slashes:
/pattern/flags
Example:
/[0-9]+/g
Breakdown:
| Part | Meaning |
|---|---|
/ |
Regex starts |
[0-9]+ |
Pattern |
/ |
Regex ends |
g |
Flag (global) |
π― 2. Character Classes β The Heart of Regex
The most important part of regex:
β [ ] β Character Set
Whatever you put inside the brackets will match any one of those characters.
Examples:
| Pattern | Meaning |
|---|---|
[0-9] |
Any digit |
[a-z] |
a to z |
[A-Z] |
A to Z |
[a-zA-Z] |
Alphabets only |
[0-9-] |
Number or hyphen |
π‘ Just remember these 5 β youβve already learned 60% of regex!
π― 3. Quantifiers β How Many Times?
Quantifiers decide how many times a pattern should repeat:
| Symbol | Meaning | Example | Explanation |
|---|---|---|---|
+ |
1 or more | [0-9]+ |
One or more digits |
* |
0 or more | a* |
"a" may or may not exist |
? |
0 or 1 | colou?r |
color OR colour |
π― 4. Anchors β Start & End of String
| Anchor | Meaning |
|---|---|
^ |
String starts with |
$ |
String ends with |
Example:
/^[0-9]+/
β String must start with a number.
π― 5. Flags β Extra Power
Flags come after the closing slash.
| Flag | Name | Use |
|---|---|---|
| g | Global | Find ALL matches |
| i | Ignore case | Case-insensitive matching |
| m | Multiline | Applies to each line |
| s | Dotall |
. also matches newline |
Example:
/hello/gi
Matches:
- hello
- Hello
- HELLO
- hElLo
π― 6. Real-World Practical Examples
β Example 1: Remove Numbers
"50-200 metric tonnes".replace(/[0-9]/g, "");
Output:
-metric tonnes
β Example 2: Extract Only Numbers (including hyphen)
"50-200 metric tonnes".match(/[0-9-]+/)[0];
Output:
50-200
β Example 3: Validate Email
/^[\w.-]+@[\w.-]+\.\w{2,}$/i.test("user@example.com");
β Example 4: Remove Special Characters
text.replace(/[^a-zA-Z0-9 ]/g, "");
β Only letters, numbers, and spaces remain.
π― 7. Regex vs No Regex
β Without Regex
let result = "";
for (let ch of str) {
if (!"0123456789".includes(ch)) {
result += ch;
}
}
β With Regex
str.replace(/[0-9]/g, "");
β Shorter
β Cleaner
β Readable
π Final Tips to Remember Regex Forever
Regex is not magic β itβs just pattern matching.
Remember these 4 rules:
-
[ ]β What to match -
+ * ?β How many times -
^ $β Start / End -
g i m sβ Flags (behavior)
If you remember these, regex becomes easy.
π Conclusion
Regex is powerful but NOT complicated.
Once you understand the basics, you can:
- Clean strings
- Validate user input
- Extract data
- Perform replacements easily
Regex = Pattern + Repetition + Flags
Master this formula and regex will feel simple forever. π
Top comments (0)