RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Pattern Title
Expression
src[^>]*[^/].(?:jpg|bmp|gif)(?:\"|\')
Description
This expression will return the src (source) property of an image tag <IMG>. The source returned is limited to the file extensions supplied. It doesn't matter if the image is prefixed with '../' or 'http://..blah' or 'drive\path' or nothing. This expression could be modifed to return other sources or for the tags (ie <BGSOUND>, <SCRIPT>, etc.) depending on the file extensions given. I needed this to replace the source of images in a HTML page when I wanted to save the page to a local drive and keep the images intact. If anybody could modify this further to include only the <IMG> tag, I would appreciate the effort.
Matches
src="../images/image.jpg" | src="http://domain.com/images/image.jpg" | src='d:\w
Non-Matches
src="../images/image.tif" | src="cid:value"
Author Rating: The rating for this expression. Lloyd Sturge
Source
Your Rating
Bad Good

Enter New Comment

Title
 
Name
 
Comment
 
Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: Nope
Name: Apparent Idiot
Date: 11/30/2008 3:11:08 AM
Comment:
Doesn't work at all.


Title: aspx/php/asp/... urls?
Name: Genius
Date: 6/29/2005 11:36:19 AM
Comment:
good RE pattern, but it matching url's with server code like this: <img src="icanhackyou.aspx?somestr=bmp"> imho, it's not correct


Title: modified
Name: sj26
Date: 11/14/2004 3:04:07 AM
Comment:
slight modification to allow spaces between the attribute name (src) and the equal sign (=) and value (("|')...("|')), and also betwen the '/' (if present) and '>': \<\s*img\s+[^>]*?src\s*=\s*('|")(.*?)\1[^>]*?\/?\s*\>


Title: Match <img> tags only
Name: sj26
Date: 11/14/2004 2:59:38 AM
Comment:
matching <img> tags only: \<\s*img\s+[^>]*?src=('|")(.*?)\1[^>]*?\/?\> You'll need param 2 (\2); param 1 is for consitent quoting (ending quote is same as beginning). Don't forget to use single quotes ('; need to escape these then) or escape backslashes (\) when using PHP!


Title: Bug
Name: sj26
Date: 11/14/2004 2:55:16 AM
Comment:
You have an error in your expression: the dot '.' needs to be escaped otherwise it matches any character (ie src="blahblahblahjpg" works, the dot '.' matchs the 'h' from the last blah). -- Sam


Title: hint fpr php users
Name: d gasser
Date: 7/21/2004 6:20:28 AM
Comment:
If the expression is used in php, you have to add a backslash in front of the slash in the statement.


Title: Match IMG tag but return SRC property
Name: Lloyd Sturge
Date: 1/23/2004 7:54:34 AM
Comment:
Thanks David Rolston for your modifications! I would like to know if there is a way to match the IMG tag but just return the SRC property value. This would eliminate matching other potential tags that also have the SRC property. If this can be done, what changes would be needed for the above regular expression to match any file extension?


Title: Modified src=
Name: David Rolston
Date: 1/22/2004 8:13:06 PM
Comment:
I think this adds the <img requirement you're looking for. This does suppose that a case insensitive modifier would be applied. <img[ ]+[^>]*src[^>]*[^/].(?:jpg|bmp|gif)(?:\"|\')


Copyright © 2001-2018, RegexAdvice.com | ASP.NET Tutorials