DEV Community

loading...

Algorithm 101: 9 Ways to Search and Replace a Word

NJOKU SAMSON EBERE
I love providing solutions to real world problems. When I am not coding, I enjoy movies, hanging out and working out.
Updated on ・2 min read

In how many ways can you search and replace a word in a sentence?


searchAndReplace("Njoku Samson Plenty", "Plenty", "Ebere"); // 'Njoku Samson Ebere'

Enter fullscreen mode Exit fullscreen mode

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);
      }
Enter fullscreen mode Exit fullscreen mode
  • .slit() and join()
      function searchAndReplace(string, word, replacement) {
        return string.split(word).join(replacement);
      }
Enter fullscreen mode Exit fullscreen mode
  • .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(" ");
      }
Enter fullscreen mode Exit fullscreen mode
  • .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(" ");
      }
Enter fullscreen mode Exit fullscreen mode
  • 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(" ");
      }
Enter fullscreen mode Exit fullscreen mode
  • 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(" ");
      }
Enter fullscreen mode Exit fullscreen mode
  • 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(" ");
      }
Enter fullscreen mode Exit fullscreen mode
  • 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(" ");
      }
Enter fullscreen mode Exit fullscreen mode
  • 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(" ");
      }
Enter fullscreen mode Exit fullscreen mode

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.

Twitter | LinkedIn | Github

Thank You For Your Time.

Discussion (0)