Jonathan Clark
Jonathan Clark

Reputation: 20538

Clean html code with regex and javascript

How can I "clean away" all but the word dog from the html code below using Javascript and perhaps regex? The id will vary.

<div class="tag" id="11">dog</div>

Upvotes: 0

Views: 424

Answers (3)

Ben
Ben

Reputation: 1787

If it's always going to be class=tag, then a bit of jquery can help with this:

$('.tag').html()

If it's in a string already

var s = '<div class="tag" id="11">dog</div>';
$(s,'.tag').html()

Upvotes: 1

kennebec
kennebec

Reputation: 104780

ids should not begin with a digit.

<div class="tag" id="d11">dog</div>

var who= document.getElementById('d11');
alert(who.textContent || who.innerText || '');

Upvotes: 2

xkeshav
xkeshav

Reputation: 54022

although its very bad idea to parse html via js , but if you want then try this

<.*>(.*)<\/.*>

DEMO

Upvotes: 3

Related Questions