How to replace arbitrary groups of characters in javascript with a single character?

So I have a line that looks like this:

I+am++a+++great++coder

      

How do I remove my + in such a way that there is only one space left in each instance? I've tried using regex but hasn't helped so far.

+3


source to share


1 answer


You can achieve the desired result either by splitting the string into each group +

and concatenating it with one space.

var str = 'I+am++a+++great++coder',
    res = str.split(/\++/g).join(' ');
    
    console.log(res);
      

Run code




or just replace each group +

with one space.

var str = 'I+am++a+++great++coder',
    res = str.replace(/\++/g, ' ');
    
    console.log(res);
      

Run code


+5


source







All Articles