Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I Remove particular dublicate characters from string in javascript?

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: )

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading