最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to extract content of html tags from a string using javascript or angularjs? - Stack Overflow

matteradmin3PV0评论

I have a string containing content in some html tags and also some text alone.

It looks like this :

var str =  "<p></p><p><i>blabla</i></p><p><i><b>blaaaaaablaaaaa</b></i></p><iframe src="..." height="111" width="333"></iframe><p></p><p><sub>hello blabla</sub></p><p><br></p><iframe src="..." height="444" width="888"></iframe>"

I would like to extract somehow in Javascript or AngularJS only some tag (including content and attributes) then put them into an array.

For example, if I want only <iframe> tag, I should have this :

var array = ["<iframe src='...' height='111' width='333'></iframe>", "<iframe src='...' height='444' width='888'></iframe>"];

If I want <p> tag then :

var array = ["", "<i>blabla</i>","<i><b>blaaaaaablaaaaa</b></i>","","<sub>hello blabla</sub>","<br>"];

Any suggestion ? Thanks !

NOTE : Don't give an answer using Jquery please !

I have a string containing content in some html tags and also some text alone.

It looks like this :

var str =  "<p></p><p><i>blabla</i></p><p><i><b>blaaaaaablaaaaa</b></i></p><iframe src="..." height="111" width="333"></iframe><p></p><p><sub>hello blabla</sub></p><p><br></p><iframe src="..." height="444" width="888"></iframe>"

I would like to extract somehow in Javascript or AngularJS only some tag (including content and attributes) then put them into an array.

For example, if I want only <iframe> tag, I should have this :

var array = ["<iframe src='...' height='111' width='333'></iframe>", "<iframe src='...' height='444' width='888'></iframe>"];

If I want <p> tag then :

var array = ["", "<i>blabla</i>","<i><b>blaaaaaablaaaaa</b></i>","","<sub>hello blabla</sub>","<br>"];

Any suggestion ? Thanks !

NOTE : Don't give an answer using Jquery please !

Share Improve this question edited Jun 8, 2016 at 12:37 Emidomenge asked Jun 8, 2016 at 11:55 EmidomengeEmidomenge 1,54420 silver badges30 bronze badges 4
  • I believe this would fall under HTML parsing. So I would search SO for that. It's kind of a pain in the arse. Otherwise see if someone has good implementation in Regex. – Callum Linington Commented Jun 8, 2016 at 11:57
  • stackoverflow./questions/9189338/… Check this out.. here is your answer. – Tirthraj Barot Commented Jun 8, 2016 at 11:58
  • Does extract mean also remove from string? Do you need strings of iframes or can they be dom elements also? what are you doing with the results? – charlietfl Commented Jun 8, 2016 at 12:12
  • @charlietfl I need strings of iframes yes, like this : "<iframe src='...' width='...' height='....' ></iframe>" – Emidomenge Commented Jun 8, 2016 at 12:17
Add a ment  | 

4 Answers 4

Reset to default 3

You could create an angular element and get the text out of it.

Example:

$scope.array =[];
$scope.eles=
angular.element(str).find('iframe');
[].forEach.call($scope.eles, function (ctl) {
    $scope.name.push(angular.element(ctl).text())
});

here is a demo: http://jsfiddle/Lvc0u55v/5122/


Edit To get all the html of the tag you can do:

angular.element(str).find('iframe');
    [].forEach.call($scope.eles, function (ctl) {
        $scope.name.push(ctl.outerHTML)
    });

demo: http://jsfiddle/Lvc0u55v/5123/

try this code:

var iFrameArr= str.match("<iframe>(.*)<iframe>");
var iFrameContent = iFrameArr[1];

You will want to look at splitting up the string with regex using a filter such as '(<iframe>).*(<\/iframe>)'. This will find the tags of and as well as everything in between, putting it into a capture group per iteration found.

Set the html content to the DOM and extract the iframe tag using jquery

Assuming you have a div with id='test' in your DOM

var str = "<div> .... </div> <iframe src=".....">..</iframe> 
  text blabla 
  <iframe src="....."> ....blabla2.... </iframe> 
  ....
  <p>.....</p> 
  ......";
$('#test').html(str);

var ar=[]
$('#test iframe').each(function() {
    var x = $(this).wrap('<p/>').parent().html();//wrap the iframe with element p and get the html of the parent
    ar.push(x)//add the html content to array
});

//now ar contains the expected output
Post a comment

comment list (0)

  1. No comments so far