Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to make filter and word replacement method - JavaScript?
In JavaScript, there's no built-in method to replace all occurrences of a word in a string. You can create custom functions using methods like split() and join(), or use modern approaches like replaceAll().
Let's explore different methods to filter and replace words in a string:
var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript";
Method 1: Using split() and join()
This method splits the string by the target word and joins the parts with the replacement:
var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript";
console.log("Original:", sentence);
function replaceWordWithSplitJoin(word, sentence, replaceValue) {
return sentence.split(word).join(replaceValue);
}
var result = replaceWordWithSplitJoin("Yes", sentence, "Hi");
console.log("Modified:", result);
Original: Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript Modified: Hi, My Name is John Smith. I live in US. Hi, My Favourite Subject is JavaScript
Method 2: Using Regular Expression with replace()
Regular expressions provide more control over word matching and replacement:
var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript";
function replaceWordWithRegex(word, sentence, replaceValue) {
var regex = new RegExp(word, 'g'); // 'g' for global replacement
return sentence.replace(regex, replaceValue);
}
var result = replaceWordWithRegex("Yes", sentence, "Hello");
console.log("With Regex:", result);
With Regex: Hello, My Name is John Smith. I live in US. Hello, My Favourite Subject is JavaScript
Method 3: Using replaceAll() (ES2021)
Modern JavaScript provides the replaceAll() method for simpler word replacement:
var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript";
function replaceWordModern(word, sentence, replaceValue) {
return sentence.replaceAll(word, replaceValue);
}
var result = replaceWordModern("Yes", sentence, "Absolutely");
console.log("With replaceAll:", result);
With replaceAll: Absolutely, My Name is John Smith. I live in US. Absolutely, My Favourite Subject is JavaScript
Advanced Filter and Replace Function
Here's a more comprehensive function that can filter and replace multiple words:
function filterAndReplace(sentence, replacements) {
let result = sentence;
for (let word in replacements) {
result = result.split(word).join(replacements[word]);
}
return result;
}
var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript";
var replacements = {
"Yes": "Certainly",
"John Smith": "Jane Doe",
"US": "Canada"
};
var result = filterAndReplace(sentence, replacements);
console.log("Multiple replacements:", result);
Multiple replacements: Certainly, My Name is Jane Doe. I live in Canada. Certainly, My Favourite Subject is JavaScript
Comparison
| Method | Browser Support | Performance | Flexibility |
|---|---|---|---|
| split() + join() | All browsers | Good | Basic |
| Regular Expression | All browsers | Good | High |
| replaceAll() | ES2021+ | Best | Medium |
Conclusion
For simple word replacement, use split() and join() or replaceAll(). For complex patterns and case-insensitive matching, regular expressions provide the most flexibility and control.
