Matching urls without specific delimiters
I'm trying to match all the URLs inside an arbitrary text, with no specific delimiters, and eventually with multiple items in the same line:
http://www.site.com/image1.jpg "http://www.site.com/image2.jpg"
'http://www.site.com/image1.jpg&a=1'
Please not the space after the fist URL, and the terminating &a=1
This is my actual regex: (https?:\/\/.*\.(?:png|jpg))
, that matches correctly just the last URL, but the fist and second are matched as one result.
The expected result should be instead this:
http://www.site.com/ima开发者_如何学JAVAge1.jpg
http://www.site.com/image2.jpg
http://www.site.com/image1.jpg
Thanks.
if they are all ending with .jpg it's fairly easy to achieve with this regexp: /[a-zA-Z0-9:\/\.-]+\.jpg/
.
I've added few more url examples to test the match. The result is an array of matches.
var str = "http://www.site.com/image1.jpg \"http://www.site.com/image2.jpg\" 'http://www.site.com/image1.jpg&a=1' http://www.21d1dk1kk1.org/image.jpg http://www.21d1dk1-kk1.org/image.jpg";
var matches = str.match(/[a-zA-Z0-9:\/\.-]+\.jpg/g);
if you need to match more then just jpg you can use (jpg|gif|png)
etc. in place of the jpg (eg. [a-zA-Z0-9:\/\.-]+\.(jpg|gif|png)/g
)
Having the array of matches you can output it whatever way you want by iterating it. But I assume you know how to deal with that.
精彩评论