werty1001
werty1001

Reputation: 25

Replace tabs and newline in html tags which start on some text

I have some HTML output code, example part:

<div class="header">
  \n\t
  <div class="header-inner">
    \n\t\t
    <div class="title">
      Title\n\t\t
    </div>
    \n\t
  </div>
  \n
</div>

Div .title have newline and tabs before it close. It's possible delete it (replace with JS pattern)? Text random, not always "Title", and tags with the same problem can be a lot.

Need:

<div class="header">
  \n\t
  <div class="header-inner">
    \n\t\t
    <div class="title">
      Title
    </div>
    \n\t
  </div>
  \n
</div>

Upvotes: 1

Views: 346

Answers (1)

RizkiDPrast
RizkiDPrast

Reputation: 1725

please review this one

var x = `<div class="header">\n\t<div class="header-inner">\n\t\t<div class="title">Title\n\t\t</div>\n\t</div>\n</div>`;
var y = x.replace(/(\w+[\s]?)\s[\s]+/gi,function(a, b) {return a.trim();});
console.log(x, y);

Upvotes: 1

Related Questions