Joe Morgan
Joe Morgan

Reputation: 1771

VB.NET replace whitespace between HTML tags

Is there any way in VB.NET to remove all of the whitespaces between tags in HTML?

Say, I've got this:

<tr>
    <td>

The string I've built is an entire HTML document, and it counts everything before those tags as legitimate space, so I need to trim it out. Is there a reg ex or function out there I could use to do this?

Thanks

Upvotes: 0

Views: 5201

Answers (2)

motoyugota
motoyugota

Reputation:

The above solution is a good start, but the code is slightly wrong and the regular expression is more than it needs to be. Here's the minimum that you would need to do in this case:

Dim RegexObj As New Regex(">[\s]*<")

NewText = RegexObj.Replace(OldText, "><")

The \n is unnecessary because .Net includes the carriage return and line feed characters in the set of whitespace characters (\s). Not sure about other languages. And if it didn't, you would also need to include the \r character because a Windows newline is \r\n in a regex, not just \n.

Upvotes: 0

user54650
user54650

Reputation: 4426

Depending on the complexity of your document, you probably just need a replace regular expression across the document... Something like:

RegexObj.Replace(">[\s\n]*<","><")

You can read up about .NET and regular expressions here

Upvotes: 1

Related Questions