skip to Main Content

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?

2

Answers


  1. Just replace doubles with singles

    console.log(tidy("foo"));
    console.log(tidy("Re: foo"));
    console.log(tidy("Re: Re: foo"));
    console.log(tidy("Re: Re: Re: foo"));
    
    
    function tidy(input){
      while(input.indexOf("Re: Re: ")>-1){
       input = input.replaceAll("Re: Re: ", "Re: ");
      }
      return input;
    }
    Login or Signup to reply.
  2. 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).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search