../courses.php
RegEx
REGEX101
3
Find Text Patterns Without Guessing
RegEx is a pattern language for finding text that has a recognizable shape.
Why does RegEx exist?
RegEx exists because people need a compact way to find, check, extract, and replace text patterns without reading every character by hand.
RegEx means regular expression.
A regular expression is not normal text. It is a search pattern.
The pattern can be simple, like finding the word error.
The pattern can also describe a shape, like three digits, a dash, and four digits.
The goal is not to memorize symbols. The goal is to understand what each symbol asks the search engine to match.
A first safe RegEx search
const text = "error: file missing";
const pattern = /error/;
console.log(pattern.test(text)); // true
text is the content being searched.
/error/ is the RegEx pattern.
This pattern looks for the exact letters error.
test() returns true or false.
This is the safest place to start: exact text before symbols.
Search
Find matching text quickly.
Validate
Check whether input has the right shape.
Extract
Pull useful parts out of larger text.
Replace
Clean or rewrite matching text.
Guessing symbols first
Starting with exact text
Ask ChatGPT: "Does this RegEx search exact text first before using advanced symbols?"
Write a text string that contains the word error.
Create a RegEx that searches for error.
Use test() to check whether it matches.
Change the text so it does not match.
Explain why this is a pattern, not a normal string search only.
Trying to memorize every RegEx symbol at once.
Using wildcards before understanding exact matches.
Testing on only one example.
Replacing text before previewing matches.
Assuming RegEx means the same thing in every tool.
Explain RegEx as a pattern for finding text.
Lesson
AI can help, but you still check
Try it yourself
Need more help?
- ① Open ChatGPT in a new tab.
- ② Come back here and copy this lesson prompt.
- ③ Paste it into ChatGPT.
- ④ Press Enter and learn one question at a time.
You can now:
moc.rt-tij [ta] troppus
(customization available)
moc.rt-tij [ta] troppus
(customization available)