views:

540

answers:

6

I need to match a image url like this:

http://site.com/site.com/files/images/img (5).jpg

Something like this works fine:

.replace(/(http:\/\/([ \S]+\.(jpg|png|gif)))/ig, "<div style=\"background: url($1)\"></div>")

Except if I have something like this:

http://site.com/site.com/files/audio/audiofile.mp3 http://site.com/site.com/files/images/img (5).jpg

How do I match only the image?

Thanks!

Edit: And I'm using javascript.

A: 

Why not:

/([^/]+\.(jpg|png|gif))$
Thanatos
Thanks but I need the entire url not just the file name.
Mark
A: 

Assuming images will always be in the 'images' directory, try:

http://.*/images/(.*?).(jpe?g|gif|png)

If you can't assume an images directory:

http://.*/(.*?).(jpe?g|gif|png)

Group 1 and 2 should have what you want (file name and extension).

I tested the regular expression here and here and it appears to do what you want.

Nick Presta
Thanks but I can't assume the images directory. The image could be coming from anywhere. :(
Mark
Not a huge deal. I suggest you test this regex (use the link I posted). It should give you an idea of what information you will get.
Nick Presta
Thanks, but that's very similar to what I have. It matches the entire line of: http://site.com/site.com/files/audio/audiofile.mp3 http://site.com/site.com/files/images/img (5).jpg rather than just the image.
Mark
Yes, it matches the line, but the captured groups will grab the image (img (5), and jpg). What is wrong with matching the line?
Nick Presta
I'm trying to match the url of the image. I've updated my example to show more clearly what I'm trying to do.
Mark
+2  A: 

Proper URLs should not have spaces in them, they should have %20 or a plus '+' instead. If you had them written with those alternatives then your matching would be much easier.

John Kugelman
Sorry, but I can't make all my users edit their files with spaces to include %20.
Mark
@mark... your users shouldn't rename the files.. whatever you use to get the file from the users should be doing it as the files come in.
Ape-inago
A: 

Using

http:\/\/.*\/(.*)\.(jpg|png|gif)

should do the trick if all you want is the name of the image. The first group is the file name and the second group is the file extension.

laz
Thanks, but I need to match the entire url.
Mark
You should probably mention that above as it confused a few of us.
laz
A: 

Can you assume that the urls will be space delimited, or return delimited?

As in, can you assume this input?

site.com/images/images/lol (5).jpg
site.com/images/other/radio.mp3
site.com/images/images/copter (3).jpg

If you are going to have your delimiter as part of your string to return, things get tricky. What kind of volume are you talking about here? Could you do it semi-manually at all, or does the process have to be automated?

glasnt
Yes there will always be a white space after the file extension. And I know how horrible it is to have that in the url itself.Perhaps a 2 step process to make the spaces first into %20 might be a good idea.
Mark
That's what I'm thinking. Unless you have a definite separator between your image stirngs, like \n, you can't distinguish between site.com\images\img and (5).jpg
glasnt
Thanks, do you have a good way to convert the spaces? I think what I was trying to do in my answer is glitchy.
Mark
maybe just convert any space that doesn't follow with 'site.com' with %20?
glasnt
A: 

Going on what was said about spaces, I did something like this:

.replace(/\/(.*) (.*)(?=\.(jpe?g|png|gif))/ig, "/$1%20$2")

and then it became possible to use this:

http:\/\/([^\s]+\.(?:png|gif|jpg|jpeg))

I'm a little unsure about edge cases, but so far this is ok where I've tested it.

I'm sure my regex can be improved though.

Mark
Sighs, does not work when there are multiple spaces in the name. :(
Mark