Find, filter, replace: Regex as an ally

  



Regular expressions (often abbreviated as regex) are powerful tools for searching and manipulating text.
Simply put, a regex is a sequence of special characters (or symbols) that defines a pattern to find within a string.

Imagine wanting to find all numbers in a text, or all email addresses, or maybe replace certain words: regex is made for exactly that!

🔗 Enjoying Techelopment? Take a look at the website for all the details!

✨ Practical Examples

Here are some real-world scenarios where you might use regular expressions:

🔍 1. Find all emails in a document

Got a long text and want to extract all the emails written in it?
With a regex, you can say: “find everything that looks like an email address.”

Example:

From "Email mario@email.it or lucia.work@gmail.com"
→ you get: mario@email.it and lucia.work@gmail.com

✅ 2. Check if a field is a valid password

Want the password to be at least 8 characters long, have one letter and one number?
A regex can check it all at once.

Example:

If the user types “password123”, the regex says OK✅.
If they type only “abcdefg” or just “12345678”, it says NO❌.

🔄 3. Replace all bad words with “***”

Got a chat app and want to censor certain words?
A regex lets you find them anywhere in the sentence, even written in sneaky ways.

Example:

From "This is a bad word""This is a ***"

📊 4. Extract numbers from a text

Want to find all numbers in a paragraph?

Example:

From "You have 2 new messages and 15 notifications."
→ you get: 2 and 15

✂️ 5. Split a text into sentences or words

Want to split a text into sentences or words to analyze them?
Regex can detect spaces, punctuation, symbols, etc.


🔤 Basic Syntax

Regular expressions are made of special symbols used to define what to search in text. 

For example the symbol . matches any character and the symbol *  matches repeated elements, so if you want to find words starting with "a" and ending with "e", just use a regex like: a.*e

Here are the fundamental regex symbols:

Symbol Meaning Example
. Any single character a.b → "acb"
* Zero or more repetitions lo*l → "ll", "lol"
+ One or more repetitions lo+l → "lol"
? Zero or one repetition colou?r → "color", "colour"
\d Any digit (0-9) \d\d → "23"
\w Letter or digit \w\w\w → "abc"
[...] One of the specified characters [aeiou] → a vowel
^ Start of string ^Hello
$ End of string world$

🖥️ How to manage regex in JavaScript

Regular expressions are a powerful tool in JavaScript for searching, analyzing, and manipulating text strings.

1. Creating a RegExp

You can use either the literal syntax or the constructor:

const regex1 = /hello/;
const regex2 = new RegExp("hello");

2. Common Methods

  • test(): returns true/false
  • match(): returns the found occurrence
  • replace(): replaces text
  • split(): splits a string based on a pattern
const regex = /hello/;
regex.test("hello world"); // true

3. Available Flags

  • g: global (find all matches)
  • i: case-insensitive
  • m: multiline
const regex = /hello/gi;
"hello Hello".match(regex); // ["hello", "Hello"]

4. Easy example

const text = "The number is 123-45-6789";
const regex = /\d{3}-\d{2}-\d{4}/;
const result = text.match(regex); // ["123-45-6789"]


💻 Practical Examples in JavaScript

📧 Check if a string contains an email

const emailPattern = /\S+@\S+\.\S+/;
const text = "My email is user@example.com";

const hasEmail = emailPattern.test(text); // true
console.log("Contains email?", hasEmail);

🔢 Find all digits in a string

const numberPattern = /\d+/g;
const input = "The year is 2025 and the month is 04";

const numbers = input.match(numberPattern); // ["2025", "04"]
console.log("Numbers found:", numbers);

✏️ Replace all vowels with “*”

const vowelPattern = /[aeiou]/gi;
const message = "Hello World";

const censored = message.replace(vowelPattern, "*");
console.log("Censored message:", censored); // "H*ll* W*rld"

🔐 Check if a password is valid

Rules: at least 8 characters, at least one letter and one number.

const passwordPattern = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/;
const password = "abc12345";

const isValid = passwordPattern.test(password); // true
console.log("Password is valid?", isValid);

🎯 Conclusion

Regular expressions are an extremely useful tool for anyone working with strings.
They may seem complex at first, but with a bit of practice they become a superpower in every developer’s toolbox.

💡 Tip: test your regex on sites like regex101.com or regexr.com


Follow me #techelopment

Official site: www.techelopment.it
facebook: Techelopment
instagram: @techelopment
X: techelopment
Bluesky: @techelopment
telegram: @techelopment_channel
whatsapp: Techelopment
youtube: @techelopment