🔍 What are regular expressions?
Regular expression is a string describing the search pattern. It consists of symbols and special characters that define what exactly we are looking for.
In simple terms:
👉 it's like smart search mask, which finds what matches a certain structure.
/hello/This expression will find the word "hello" in any text.
🛠 How does it work?
const text = "Hi, my name is Kodik!";
const result = /Кодик/.test(text);
console.log(result); // trueThe .test() method checks Is there a match? with a template. If yes, it will return true.
🔍 Line-by-line analysis:
const text = "Hi, my name is Kodik!";
We create a variable text that stores a regular string. This is our "text" in which we will search for a match.
👉 Inside the line there is the word Кодик — this is what we want to find.
const result = /Code/.test(text);
This is where the most interesting thing happens:
/Кодик/— a regular expression that searches for an exact match of the wordКодик..test(text)— a method that:accepts the string
textchecks if there is a match with the template
returns
trueif a match is found, otherwisefalse
🧠 In our case, the word Кодик is indeed in the text, so the result will be true.
console.log(result); // true
Here we are just displaying the result of the check to the console:
If
Кодикis found, the console will displaytrueIf it wasn't there, it would be
false
💬 In other words:
You tell the browser:
"Hey, see if this text contains the word 'Kodik'? If yes, tell me true"
And JavaScript, as a faithful assistant, says: ✅ Yes, there is! — true
🧪 Useful constructions
Syntax | What it does | Example |
|---|---|---|
. | Any single character |
|
* | Zero or more repetitions |
|
+ | One or more |
|
? | 0 or 1 time |
|
\d | Any number |
|
\w | Letter, number or _ |
|
^ | Start of line |
|
$ | End of line |
|
🧠 Example: email verification
const email = "test@example.com";
const pattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
console.log(pattern.test(email)); // trueWhat's happening:
^and$— the beginning and end of the line[^\s@]+— at least one character other than a space and@@— must be present\.— dot before the domain (escaped!)+— repetition again
🧠Where are regular expressions used?
💡 Scope of application | 📋 What do regulars do |
|---|---|
Form validation | They check email, passwords, phone numbers, and other input fields |
Find and replace in text | They find templates and replace them with the right ones |
Parsing logs, CSV, HTML | Extract the necessary data from rows, tags and structured files |
Writing linters and parsers | Help analyze code and find errors or stylistic deviations |
Quick search by code or documentation | Find the necessary functions, variables, templates by structure |
Regular expressions are like the language of wizards for working with text. First it scares you, then it makes you happy! Try, play, write templates. And to practice with pleasure - look in the application Code 🐾
Code is an application for learning programming: courses in HTML, CSS, JavaScript and Python with simple explanations and practice that is really memorable.
💻 Learn for pleasure: https://itcodik.com/
