Abs
Abs

Reputation: 57956

How to remove the end tag from a HTML string using JS

I want to remove the end tag of a given HTML string. I get the HTML string from the document like so:

var front = $('<div>').append($(clean_copy).contents().remove().end()).remove().html();

I now want to remove the </div> from the below:

<div id="div1" style="font-size: 20px"></div>

I am really rusty with regex but I want to try something like this:

console.log("Remove End: " + front.replace('/<(/)?div([^>]*)>/g', ''));

The above doesn't work, how can I remove any tag? Also some strings may not have an end tag.

Upvotes: 0

Views: 5410

Answers (1)

Nabor
Nabor

Reputation: 1701

try front.replace(/<\/div>$/)

It replaces your div. If you like to replace any last end tag use this front.replace(/<\/\S+>$/)

Upvotes: 6

Related Questions