Gowtham
Gowtham

Reputation: 12170

Removing html line breaks using Javascript

I'm trying to grab an element's HTML using jQuery and then post it to the server. I successfully grabbed it, but I am not able to remove the white space between the tags and the line breaks that are rendered by default. The HTML code grabbed is shown below:

<table><tbody><tr><th></th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th></tr>
                <tr><th>2nd row</th><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></tbody></table>

I would like to trim the spaces between the tags only. I've used this regular expression: str.replace(/\s+/g, ' ');. But that doesn't seem to work, any suggestions?

Upvotes: 1

Views: 94

Answers (2)

Gowtham
Gowtham

Reputation: 12170

I need to add escape character(backslash - ) character at the end of each line to wrap the string.

Upvotes: 0

Paul Draper
Paul Draper

Reputation: 83205

Currently, you are replacing all consecutive sequences of whitespace with a single space.

This is what you want:

str.replace(/>\s+</g, '><');

Upvotes: 3

Related Questions