javascript - body tag class ReGex -


this odd request. using jquery ajax request full page html content. once have content captured in object var, want grab body tag class of captured content , replace current dom body class it.

since content in var , not loaded in dom jquery useless against it. best tool can think of regex.

i have looked hours on internet , found nothing.

here best code come doesn't work...

var data = '<html class="please_god_no" id="shit"><body id="id_no" class="test22">test2 class="test1" "test3"<div id="bad" class="test5 test6">test4</div> </body></html>';  var arr = data.match(/<body class=("[\w\d]*")\sid=("[\w\d]*")>([\w\d]*)</body>/); alert(arr); 

i want class don't need or want other attributes. on how find answer helpful.

huh, harder thought, turns out things tricky when dealing body/html elements. 1 needs deal created html element so:

var s = '<html class="please_god_no"><body id="id_no" class="test22">test2 class="test1" "test3"<div id="bad" class="test5 test6">test4</div> </body></html>';  var htmlobject = document.createelement('html'); htmlobject.innerhtml = s; var klass = htmlobject.queryselector("body").classname; document.queryselector('body').classname = klass; 

jsfiddle example here

this time-consuming. there's better way this, why aren't rendering body classes want? failing that, why can't retrieve classes want via ajax? think restructuring app in future versions.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -