In how many ways can you search and replace a word in a sentence?
searchAndReplace("Njoku Samson Plenty", "Plenty", "Ebere"); // 'Njoku Samson Ebere'
I have got 9 ways to Search and Replace a Word in a given sentence or group of words.
Prerequisite
This article assumes that you have basic understanding of javascript's string and array methods.
Let's Search and Replace a Word using:
- .replace()
function searchAndReplace(string, word, replacement) {
return string.replace(word, replacement);
}
- .slit() and join()
function searchAndReplace(string, word, replacement) {
return string.split(word).join(replacement);
}
- .forEach...Loop, split,indexOf, join, ternery operator
function searchAndReplace(string, word, replacement) {
let stringArray = string.split(" ");
stringArray.forEach(element => {
element === word
? (stringArray[stringArray.indexOf(word)] = replacement)
: element;
});
return stringArray.join(" ");
}
- .map(), split,indexOf, join, ternery operator
function searchAndReplace(string, word, replacement) {
let stringArray = string.split(" ");
stringArray.map(element => {
element === word
? (stringArray[stringArray.indexOf(word)] = replacement)
: element;
});
return stringArray.join(" ");
}
- for...loop, split,indexOf, join, ternery operator
function searchAndReplace(string, word, replacement) {
let stringArray = string.split(" ");
for (let i = 0; i <= stringArray.length; i++) {
stringArray[i] === word
? (stringArray[stringArray.indexOf(word)] = replacement)
: stringArray[i];
}
return stringArray.join(" ");
}
- for...in...loop, split,indexOf, join, ternery operator
function searchAndReplace(string, word, replacement) {
let stringArray = string.split(" ");
for (element in stringArray) {
stringArray[element] === word
? (stringArray[stringArray.indexOf(word)] = replacement)
: stringArray[element];
}
return stringArray.join(" ");
}
- for...of...loop, split,indexOf, join, ternery operator
function searchAndReplace(string, word, replacement) {
let stringArray = string.split(" ");
for (element of stringArray) {
element === word
? (stringArray[stringArray.indexOf(word)] = replacement)
: element;
}
return stringArray.join(" ");
}
- RegExp(), for...of...loop, split(), match(), indexOf(), join()
function searchAndReplace(string, word, replacement) {
let stringArray = string.split(" ");
let regEx = new RegExp(word, "g");
for (element of stringArray) {
element.match(regEx)
? (stringArray[stringArray.indexOf(word)] = replacement)
: element;
}
return stringArray.join(" ");
}
- RegExp(), for...in...loop, split(), test(), indexOf(), join()
function searchAndReplace(string, word, replacement) {
let stringArray = string.split(" ");
let regEx = new RegExp(word, "g");
for (element in stringArray) {
regEx.test(stringArray[element])
? (stringArray[stringArray.indexOf(word)] = replacement)
: stringArray[element];
}
return stringArray.join(" ");
}
Conclusion
There are many ways to solve problems programmatically. You are only limited by your imagination. The Regular Expression (RegExp)
version can also be achieved using other looping construct. Why not try it out and tell us how you did it in the comment section?
I will love to know other ways you solved yours in the comment section.
Up Next: Algorithm 101: 3 Ways to Check If Two Words are Anagrams
If you have questions, comments or suggestions, please drop them in the comment section.
You can also follow and message me on social media platforms.
Thank You For Your Time.
Top comments (0)