Algorithm 101: 9 Ways to Search and Replace a Word
Tags: algorithms, javascript, search, replace
Published: 2020-03-10 06:15:30

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

```javascript

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()

```javascript
      function searchAndReplace(string, word, replacement) {
        return string.replace(word, replacement);
      }
```

* .slit() and join()

```javascript
      function searchAndReplace(string, word, replacement) {
        return string.split(word).join(replacement);
      }
```

* .forEach...Loop, split,indexOf, join, ternery operator

```javascript
      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

```javascript
      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

```javascript
      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

```javascript
      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

```javascript
      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()

```javascript
      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()

```javascript
      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](https://dev.to/ebereplenty/algorithm-101-3-ways-to-check-if-two-words-are-anagrams-1j6l)

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](https://twitter.com/eberetwit) | [LinkedIn](https://www.linkedin.com/in/samson-ebere-njoku-profile/) | [Github](https://github.com/EBEREGIT)**

Thank You For Your Time.