views:

138

answers:

3

I have WYSIWYG and I don't allow users to upload images, just links to images outside the site.

So I want to find every image link/path and replace it with some default thumb, where user can click and that image will be shown in modal window. I'm using qTip, but that will be my part of the job :)

So if there is:

http://sstatic.net/so/img/logo.png

or

http://l.yimg.com/a/i/ww/news/2010/02/22/fries_on_plate-pd.jpg

They will be "converted" to something like:

<a href="http://OriginalPathToImage" rel="tool_tip"><img src="/SomeDefaultImageThumb.jpg" /></a>

So, how to find all images? What is RegExp for finding .jpg, .gif, .png.... or some other solution?

+2  A: 

It's a little tough to say exactly what to do, since I don't know what you're searching.

If the links are currently part of a tag, it will be simple.

But it sounds like the image hrefs are not currently part of any tag. Is that right?

If so, and you think you need a reg exp, you could try,

var result = text.match(/http:\/\/\S+(\.png|\.jpg|\.gif)/g);

(This is a simple one that could I'm sure be tightened up a bit. As it is, it will not allow 'space' characters in the href, for example.)

It will search for 'http://' plus anything except a space character plus '.png' or '.jpg' or '.gif', and return an array of the results.

So if the search text was:

var text = 'http://sstatic.net/so/img/logo.png some text http://l.yimg.com/a/i/ww/news/2010/02/22/fries_on_plate-pd.jpg more text http://l.yimg.com/this/is/another/path/to_this_image.jpg';

It would return an array of 3 results.

Using jQuery (since you tagged it) you could then:

var $aTag;

for(i in result) {
    $aTag = $('<a><img src="/SomeDefaultImageThumb.jpg" /></a>').attr({'href': result[i]});
    $('body').append($aTag);
}

Which will append the new tags, in this case, to the body.

EDIT: Using your example, I added parenthesis around the search query, and referenced it with href="$1" in the replace string -

An extra set of parenthesis was added to the search string. This creates the 'memorized' group that was referenced by $1.

$("div.test").each(function() { 
    $(this).html($(this).html().replace(/(http:\/\/\S+(\.png|\.jpg|\.gif))/g, '<a href="$1"><img src="/SomeDefaultImageThumb.jpg" /></a>')); 
});

There may be security implications to this if malicious code can be hidden in the link string. Not sure though. If that's an issue, there should be another way to go about it.

EDIT: Excludes hrefs from site's domain (assuming they are part of the same text that is being searched).

var hostname = window.location.hostname.replace(/\./g,'\\.');
var re = new RegExp('(http:\\/\\/[^' + hostname + ']\\S+[\\.png|\\.jpg|\\.gif])','g');

$("div.test").each(function() {
    $(this).html($(this).html().replace(re, '<a href="$1"><img src="/SomeDefaultImageThumb.jpg" /></a>')); 
});
patrick dw
Yes that is it :) But i have small problem. Added 3 div's with class "test" and each of them have pic link, only the third have .txt That one is also appended to the body, but without extension, like: http://www.google.com/logo.And could You give me tip how to replace that link with $aTagThanks I really appreciate Your help
Kenan
Sure. But when you say that you have 3 divs and each has a pic link, what do you mean. Is the pic link the text inside of the div, or is it an attribute of the div?
patrick dw
Those are user comments and each "div.comment" could contain just link but without tags, like: "bla bla h tt p://sstatic.net/so/img/logo.png bla bla"... or whatever what user can add.
Kenan
I've updated the regular expression. I assume we are searching through text content. Let me know if it works (and remember to click the checkbox if you like my answer).
patrick dw
This is what I have managed to create so far :), and just need to add link to replaced A tags:$("div.test").each(function() { $(this).html($(this).html().replace(/http:\/\/\S+(\.png|\.jpg|\.gif)/g, '<a><img src="/SomeDefaultImageThumb.jpg" /></a>'));});
Kenan
I edited my answer with your code. My computer is freaking out, so I've got to reboot. Didn't have a chance to test it yet, but I think it works. Let me know.
patrick dw
Hi Patrick, Could You see reply bellow? In uRL's Im getting only file extensions like: <a href=".jpg"> I tried adding $0 and $2, but nothing :( Thanks again
Kenan
Did you use the **EDIT** that I placed at the bottom of my answer? There is an extra set of parentheses that was added from your solution. If you copy and past it, it should work. Notice the extra parenthesis before 'http' and after '.gif'. `(http:\/\/\S+(\.png|\.jpg|\.gif))`
patrick dw
Thank You, Thank You, Thank You :) I owe You a beer :D
Kenan
Mmmmm...beeeeerrrrr...
patrick dw
One more :$ Is there any way to exclude images where in link is my domain, for example: http//www.MyDomain.com/images/green.png Just realized that smilies are in the comments :$ Now i owe You 2 beer :D
Kenan
I think I've got it so it will exclude your domain. I'll update my answer in a minute.
patrick dw
A: 

I'm loosing my cookies at home, so I have to reply again.

I'm not getting the whole link, just extension:

<a href=".jpg">
<a href=".png">

Thanks one more time

Kenan
Kenan - Updates should be added as edits to your original question, or as comments. Reduces clutter and potential confusion for future readers.
patrick dw
A: 

RegEx? We don't need no stinkin' RegEx!

$('a[href$="jpg"], a[href$="jpeg"], a[href$="png"], a[href$="gif"]')

CSS selectors can do that for you. The above selects all a elements with an href ending with the specified formats.

Alan
We do need regEx because the hrefs are coming from text comments placed by users. The `a` elements are being created dynamically, and populated with the users' href.
patrick dw
Ah. I was under the impression that there are already links because the OP referred to the paths as such. Text to HTML conversion should really be done server-side, though.
Alan
There are no links, thats problem. Just path's, I thought about selectors... Thanks anyway,
Kenan