Javascript’s string object has a replace() method that allows you to replace characters.


  let name = 'nevin';
  let new_name = name.replace('n','k');
  console.log(new_name);

  // output: 'kevin'

You can also use replace() on whole words and other multi-character strings.


  let phrase = 'the quick brown dog';
  let new_phrase = phrase.replace('dog','cat');
  console.log(new_phrase);

  // output: 'the quick brown cat'

Unless you are using a regular expression, however, replace() only replaces the first instance of a letter or substring. If we want to replace both N’s in this string, we need another technique.

Here is the same issue with replacing a word.


  let phrase = 'the quick blue dog is the quickest of all the dogs';
  let new_phrase = phrase.replace('dog','cat');
  console.log(new_phrase);

  // output: 'the quick blue cat is the quickest of all the dogs'

No good! The result above doesn’t make much sense if we don’t replace that second instance of ‘dog.’

So to replace all letters or substrings without the use of regular expressions, we need another approach. Fortunately, we can use two methods called split(), which acts on strings, and join(), which acts on arrays.

The split() method is used to split a string into an array of substrings. How does it decide where to cut the string? It can use a specific character called a delimiter.

In the code below, we are using the dash (-) as a delimiter so that split() will cut the string at each dash.


  let string = 'terrible-stubborn-insidious-very-bad-bug';
  let array = string.split('-');
  console.log(array);

  // output: ['terrible','stubborn','insidious','very','bad','bug'];


The join() method works on arrays and can be thought of as the opposite of split(). It takes a delimiter as an argument and this time joins strings together using that delimiter.

Using join(), we can stitch the above array back together with a new delimiter. So let’s try a different delimiter, such as an underscore (_).


  let array = ['terrible','stubborn','insidious','very','bad','bug'];
  let string = array.join('_');
  console.log(string);

  // output: 'terrible_stubborn_insidious_very_bad_bug';

Now once we split() a string into an array of substrings, we can then use join right away on the same line to finally replace multiple instances of something.

Let’s try this with one of the original cases wherein replace only replaced one of the letters.


  let name = 'nevin';
  let new_name = name.split('n').join('k');
  console.log(new_name);

  // output: 'kevik'

And even though I don’t run into many Keviks these days, the logic works the way it should.

Let’s try it with the dog and cat example.


  let phrase = 'the quick blue dog is the quickest of all the dogs';
  let new_phrase = phrase.split('dog').join('cat');
  console.log(new_phrase);

  // output: 'the quick blue cat is the quickest of all the cats'

So now we see that split() and join() can allow you to replace multiple instances of a character or multi-character substring in a string. Let me know if you have any questions on this post, and I hope this helps you with your programming.