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

Regex last space remover pattern

I have a rich text editor. It generates HTML output. I want to remove last space between i and strong tags.
For example;

Input:

<strong>Hello World </strong>

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

Expected Output:

<strong>Hello World</strong>

Input:

<strong>Hello World <i>Hello Planet </i> </strong>

Expected Output:

<strong>Hello World <i>Hello Planet</i></strong>

>Solution :

/ (<\/i>|<\/strong>)/g should be replaced with ‘$1’ as shown below:

var str = '<strong>Hello World <i>Hello Planet </i> </strong>';
var patt = / (<\/i>|<\/strong>)/g;
console.log(str.replace(patt, '$1'));
// Produces: '<strong>Hello World <i>Hello Planet</i></strong>'
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