Vijay
Vijay

Reputation: 374

extract image tag from string

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;">

Upvotes: 0

Views: 2100

Answers (7)

Daisy
Daisy

Reputation: 328

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

/<img[^>]+>/

const match = string.match(/<img[^>]+>/);
const result = match[0];

Upvotes: 0

user5296864
user5296864

Reputation:

$('span.AM').html();

Please try above

Upvotes: 0

dakab
dakab

Reputation: 5875

Select the element, and match the image tag string:

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

Upvotes: 0

Christian Gollhardt
Christian Gollhardt

Reputation: 17004

//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);

Upvotes: 0

Spokey
Spokey

Reputation: 10994

Something like this

$('img')[0].outerHTML;

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82231

You can use:

var ele = $(theHtmlString);
var imgobj= $('.AM img', ele);

Upvotes: 0

adeneo
adeneo

Reputation: 318182

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');

Upvotes: 5

Related Questions