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
  • 17,588
  • 29
  • 105
  • 173
  • 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

diEcho
  • 52,196
  • 40
  • 166
  • 239
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
  • 98,993
  • 30
  • 103
  • 125
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,417
  • 3
  • 21
  • 27
Ben
  • 1,778
  • 16
  • 32