0

I have following html

<div> <div class="first"> First Second </div>  <div class="second">Second</div>  <div class="third">Third</div> <div>

I want this output after removing white spaces.

First SecondSecondThird

i have used this code

    var str = " <div class="first"> First Second </div> <div class="second"> Second</div> <div class="third">Third</div>"; 
   str=  str.replace(/\s+/g, " ").trim();

Any idea how it will work. i need this in pure javascript or anglurjs

0

1 Answer 1

1

You can use regex /(>)\s+|\s+(<)/g

var str = ' <div class="first"> First Second </div> <div class="second"> Second</div> <div class="third">Third</div>';
str = str.replace(/(>)\s+|\s+(<)/g, "$1$2");
document.write(str)
div {
  display: inline-block
}


Regex explanation here.

Regular expression visualization

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man this is working like a charm . Can you please explain this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.