0

Sample :

    <p>
        <span class="AM"> *** 
            <img src="image" title="title" style="vertical-align: middle;">***  
        </span> 
        TF1: People needs to learn writting when they are young 
    </p> .

I 

need to extract the image tag fully(not only the resource attribute) from the string.

expected result :

<img src="image" title="title" style="vertical-align: middle;">
Vijay
  • 344
  • 2
  • 11

7 Answers7

5

Just target the image ?

$('.AM img')

If you need the outer HTML as a string

$('.AM img').get(0).outerHTML

and if all that is a string, pass it to jQuery to parse it

$(string).find('.AM img');
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • In my scenario i'd set the string in one variable need to extract the image tag from that. eg : var string = "

    *** *** TF1: People needs to learn writting when they are young

    ."; thanks @adeneo
    – Vijay May 09 '14 at 08:08
0

You can use:

var ele = $(theHtmlString);
var imgobj= $('.AM img', ele);
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

Something like this

$('img')[0].outerHTML;
Spokey
  • 10,974
  • 2
  • 28
  • 44
0

//Edit: Thought this is a PHP Question.

In PHP you do it so:

$out = null;
preg_match('<img([\w\W]+?)/>', $html, $out);
print_r($out);
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
  • time for [**Tony the Pony**](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) again – adeneo May 09 '14 at 08:03
  • Sometimes you can match some specific HTML, you don’t have to actually parse it. But `\w\W` is not optimal, since you have to look for a `>`. – dakab May 09 '14 at 08:06
0

Select the element, and match the image tag string:

document.getElementsByTagName('p').item(0).innerHTML.match(/(<img[^>]+>)/g);

dakab
  • 5,379
  • 9
  • 43
  • 67
0
$('span.AM').html();

Please try above

Rohit Batham
  • 1,238
  • 1
  • 9
  • 13
0

Use regular expression to get the image element. the expression will be

/<img[^>]+>/

const match = string.match(/<img[^>]+>/);
const result = match[0];
Daisy
  • 288
  • 3
  • 8