javascript - Check if a text is HTML or not -
i using meteor , trying check if text html. usual ways not work. code:
post: function() { var postid = session.get("postid"); var post = posts.findone({ _id: postid }); var object = new object(); if (post) { object.title = post.title; if ($(post.content).has("p")) { //$(post.content).has("p") / post.content instanceof htmlelement object.text = $(post.content).text(); if (post.content.match(/<img src="(.*?)"/)) { object.image = post.content.match(/<img src="(.*?)"/)[1]; } } else { console.log("it not html------------------------"); object.text = post.content; } } return object; }
actually, "working" solution have used now. also, pointed out 2 common ways use (next if statement). possible happen without regex.
can use approach started jquery append response new <div>
, check if element has children. if jquery finds children html.
if html can search div type of element using find()
.
// create element , inject content var $div=$('<div>').html(post.content); // if there children html if($div.children().length){ console.log('this html'); var $img = $div.find('img'); console.log('there ' + $img.length +' image(s)'); }else{ console.log('this not html'); }
Comments
Post a Comment