I’m doing a mail project and it has reply function to particular email. There are pre-written things in reply email like Re: ${Subject of the email}
There is Re: that repeats after 2nd reply so i wrote this in my function in order to remove it:
subject = document.querySelector('#compose-subject').value;
if (subject.includes("Re: ")){
subject = subject.replace("Re: ", "");
}
How do i make this part of code work only for dublicates? Like Re: Re: (removing 2nd Re: )
Now it works even on first Re: and just removing it.
How can i implement it?
>Solution :
You can use a regexp to replace any number of repeated Re:s with any number of spaces after them with a single Re: :
var subject = document.querySelector('#compose-subject').value;
subject = subject.replace(/^(Re:\s+)+/g, 'Re: ');
You can add the i flag for case insensitivity too (i.e. /gi instead of /g).