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 to move spaces after or before certain tags

I use Quill.js editor for editing text, I have bold and italic button but the problem that I need to transform html to markdown. For correct markdown I need that there are no spaces before or after strong or em tag, the best place I think will be to move spaces in html string
For example
let html = '<p><strong>some </strong><em>text</em> <strong>is </strong><em>here</em></p>';
The result must be
let html = '<p><strong>some</strong> <em>text</em> <strong>is</strong> <em>here</em></p>';
The space before close tag is moved after
How can I achive this result?

I try to split string for spaces and there get first tags but I can’t finish because I think I need some regExp

`const tags = ['strong', 'em']
 let newHtml = ''

 tags.forEach(tag => {
  let splitOnMarker = html.split(' ')
 })

let result = arr.reduce((acc, item) => {
  item = item.replace(/(<\/strong>).*/, '</strong> ');
  acc += item;
  return acc;
}, '');`

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

>Solution :

You can do a simple replacement like so:

let html = '<p><strong>some </strong><em>text</em> <strong>is </strong><em>here</em></p>';
    console.log(html);
    html = html.replace(/ <\/strong>/g, '</strong> ');
    html = html.replace(/ <\/em>/g, '</em> ');
    html = html.replace(/<strong> /g, ' <strong>');
    html = html.replace(/<em> /g, ' <em>');
    console.log(html);
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