14

I have this string:

hello world hello world hello world hello

and I need to get the following:

hello world hello hello hello

If I use:

str = str.replace('world', '');

it only removes the first occurrence of world in the above string.

How can I replace all the occurrences of it except the first one?

Kristianmitk
  • 4,528
  • 5
  • 26
  • 46
karlosuccess
  • 843
  • 1
  • 9
  • 25

5 Answers5

20

You can pass a function to String#replace, where you can specify to omit replacing the first occurrence. Also make your first parameter of replace a regex to match all occurrences.

Demo

let str = 'hello world hello world hello world hello',
    i = 0;
    
str = str.replace(/world/g, m  => !i++ ? m : '');
console.log(str);

Note

You could avoid using the global counter variable i by using a IIFE:

let str = 'hello world hello world hello world hello';

str = str.replace(/world/g, (i => m => !i++ ? m : '')(0));
console.log(str);
Kristianmitk
  • 4,528
  • 5
  • 26
  • 46
  • You don't need to use regex for simple cases as in the question, you can match a string with `replaceAll`: `str.replaceAll('world', m => !i++ ? m : '');` – Rafael Tavares Jun 07 '21 at 14:19
  • just bear in mind that replaceAll is not nativelly supported in nodeJS version < 15. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll#browser_compatibility – Kristianmitk Sep 29 '21 at 07:25
  • That's true. For future readers: you can use `replaceAll` in some situations and add a polyfill if needed, as I stated in that answer: [Jest: TypeError: replaceAll is not a function](https://stackoverflow.com/a/67877325/8839059) – Rafael Tavares Sep 29 '21 at 12:10
2

To provide an alternative to @Kristianmitk excellent answer, we can use a positive lookbehind, which is supported in Node.Js & Chrome >= 62

const string = 'hello world hello world hello world hello';

console.log(
  string.replace(/(?<=world[\s\S]+)world/g, '')
);
// or
console.log(
  string.replace(/(?<=(world)[\s\S]+)\1/g, '')
);

Using Symbol.replace well-known symbol.

The Symbol.replace well-known symbol specifies the method that replaces matched substrings of a string. This function is called by the String.prototype.replace() method.

const string = 'hello world hello world hello world hello';

class ReplaceButFirst {
     constructor(word, replace = '') {
         this.count = 0;
         this.replace = replace;
         this.pattern = new RegExp(word, 'g');
     }
     
     [Symbol.replace](str) {
          return str.replace(this.pattern, m => !this.count++ ? m : this.replace);
     }
}

console.log(
  string.replace(new ReplaceButFirst('world'))
);
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
1

In my solution, I am replacing first occurrence with a current timestamp, then replacing all occurrences and then finally replacing timestamp with world

You can also use str.split('world') and then join

var str = 'hello world hello world hello world hello';
var strs = str.split('world');
str = strs[0] + 'world' + strs.slice(1).join('');
console.log(str);

var str = 'hello world hello world hello world hello';

const d = Date.now()
str = str.replace('world', d).replace(/world/gi, '').replace(d, 'world');

console.log(str);
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
1

var str = 'hello world hello world hello world hello'; 

var count = 0;
var result = str.replace(/world/gi, function (x) {
  if(count == 0) {
      count++;
      return x;
    } else {
      return ''; 
    }
});

console.log(result);
Rama Adaikkalam
  • 665
  • 1
  • 7
  • 14
0

I would do it like this:

  1. Get the substring up until and including the first occurrence.
  2. Append to that the substring after the first occurrence, with all other occurrences removed:

    function replaceExceptFirst(str, search) {
         let index = str.indexOf(search);
         return str.substring(0, index + search.length) + 
             str.substring(index + search.length).replace(/world/g, '')
    }
    
    console.log(replaceExceptFirst('hello world hello world world hello', 'world'))
dave
  • 62,300
  • 5
  • 72
  • 93