JavaScript replace() Function

🔍 What is replace()?

JavaScript’s replace() function lets you search for a string or pattern and swap it with something new. Simple, right? But it gets way more powerful when you dive deeper.

string.replace(searchValue, newValue)
  • searchValue – a string or regex pattern to look for
  • newValue – the replacement string or function

Example:

let text = "Hello world!";
let newText = text.replace("world", "JavaScript");
console.log(newText); // "Hello JavaScript!"

🧙🏽‍♂️ Going Beyond: Regex Power

Regular expressions (regex) allow dynamic pattern matching. For example:

let text = "cat, bat, sat";
let newText = text.replace(/at/g, "og");
console.log(newText); // "cog, bog, sog"

The /at/g regex matches all “at” occurrences (g means global).


💡 Advanced Techniques

1. Dynamic Replacements:

Use a function to transform matches on the fly:

let numbers = "5, 10, 15";
let doubled = numbers.replace(/\d+/g, match => match * 2);
console.log(doubled); // "10, 20, 30"

2. Named Capturing Groups (ES2018+):

Format dates using named groups for better readability:

let date = "2024-10-10";
let formatted = date.replace(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/, '$<day>/$<month>/$<year>');
console.log(formatted); // "10/10/2024"

🚀 Why Master replace()?

  • 🧹 Clean and format strings easily
  • 🧠 Leverage regex for powerful pattern matching
  • 🧾 Write less code to do more work

✅ Conclusion

The replace() function is a must-know tool for JavaScript devs. Whether you’re cleaning up user input or formatting data, it makes string handling smart and efficient.

Next time you manipulate text, remember—replace() is your Swiss Army knife.


🧠 QUIZ

1. What does the g flag do in a regex with replace()? A) Replace only the first match B) Case-insensitive match C) Replace all matches D) Ignore non-alphanumerics

2. Which code uses a function to dynamically modify values? A) text.replace("world", "JavaScript") B) text.replace(/at/g, "og") C) numbers.replace(/\d+/g, match => match * 2) D) date.replace(/(\d{4})-(\d{2})-(\d{2})/, '$3/$2/$1')


<
Previous Post
3 Reasons I Stopped Ignoring console.info() (And You Should Too)
>
Next Post
🎙📸🎥 Capture Sounds, Photos and Videos with HTML