Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use regular expression to grab an 'img' tag?

I want to grab an img tag from text returned from JSON data like that. I want to grab this from a string:

<img class="img" src="https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-frc3/1239478_598075296936250_1910331324_s.jpg" alt="" />

What is the regular expression I must use to match it?

I used the following, but it is not working.

"<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>"
like image 244
eng.ahmed Avatar asked Sep 06 '13 19:09

eng.ahmed


People also ask

What is the syntax for IMG tag?

HTML Images Syntax The <img> tag is empty, it contains attributes only, and does not have a closing tag. The <img> tag has two required attributes: src - Specifies the path to the image. alt - Specifies an alternate text for the image.

Can we use ngIf in IMG tag?

As i understood, ngIf removes element from the DOM, and hence when ngIf evaluates to falsy value, it removes the img element from the DOM, now when I toggle back to true value, it's bringing back the cached url instead of rendering fresh img element.

How do I capture a number in regex?

\d for single or multiple digit numbers It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range. [1-9][0-9] will match double digit number from 10 to 99.

How do you match expressions in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).


1 Answers

You could simply use this expression to match an img tag as in the example :

<img([\w\W]+?)/>
like image 111
aleroot Avatar answered Oct 04 '22 22:10

aleroot