0

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>
Jonathan Clark
  • 19,726
  • 29
  • 111
  • 175
  • You want to extract the text present within div? – naresh Feb 01 '12 at 18:03
  • You don't want to use regexp. Instead you want to parse HTML using DOM routines and then manipulate the DOM tree. http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Mikko Ohtamaa Feb 01 '12 at 18:07

3 Answers3

3

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

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

DEMO

xkeshav
  • 53,360
  • 44
  • 177
  • 245
2

ids should not begin with a digit.

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

var who= document.getElementById('d11');
alert(who.textContent || who.innerText || '');
kennebec
  • 102,654
  • 32
  • 106
  • 127
1

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()
dplante
  • 2,445
  • 3
  • 21
  • 27
Ben
  • 1,767
  • 16
  • 32