0

Mathew O'Rordon seems like a great guy who have written a great regEx to pick up URL's in a text. It works almost flawless. :)

But I found out one problem: If there is a img-tag with a static URL this regEx also catch it, which it should not. I am not fluent on regEx and I do not know what to do with this so it does not catch img src-URL's.

Editor with the script: http://jsbin.com/futuyobawo/edit?html,js,output

Try to paste this into the text and see what happends with the img-code:

<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/Hermann_Hesse_2.jpg/200px-Hermann_Hesse_2.jpg">

The script:

$(function() {
   var urlRegEx = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?)/g;
   $('#target').html($('#source').html().replace(urlRegEx, "<a href='$1'>$1</a>"));
});

Is there anyone who can see what I need to do to make this regEx exclude img src URL's? That might be the last bit for the ultimate URL-regEx. :)

smyslov
  • 1,279
  • 1
  • 8
  • 29
JoakimB
  • 157
  • 1
  • 11
  • the regex does nothing with the image: http://jsbin.com/huzerizuso/1/edit?html,js,output are you expecting something else? – caramba Aug 14 '15 at 12:54
  • Yes, it does do this: `` which really mess up the code. I want the regEx to not pick up URL's in img-tags. – JoakimB Aug 14 '15 at 17:37
  • One solution, may be (I am not good at regEx so I am guessing here), to look for if **="** before this regEx. If it does exist it should not pass by this regEx since URL's always have those characters before in HTML. I just do not know how to do it and if it is the right solution. – JoakimB Aug 14 '15 at 17:43
  • Try this content and the error on the img occur: `www.wikipedia.org http://www.wikipedia.org matt@google.com https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/Hermann_Hesse_2.jpg/200px-Hermann_Hesse_2.jpg http://wikipedia.org .

    Hej

    `
    – JoakimB Aug 14 '15 at 17:46
  • It is the regEx that is rendering that, it is the exact problem, and that is why I am writing here! I know valid HTML. :) – JoakimB Aug 14 '15 at 17:58
  • why is there `replace(urlRegEx, "$1")` ? try like `replace(urlRegEx, "$1")` http://jsbin.com/tibitivivu/1/edit?html,js,output – caramba Aug 14 '15 at 18:08
  • Your suggested solution above does not work and definitly do not save the IMG-tag. :) Go here: [http://jsbin.com/futuyobawo/edit?html,js,output](http://jsbin.com/futuyobawo/edit?html,js,output) In the right column (Output), check the broken image at the bottom. The image above is how I added the image to the content. The broken image is how it looks after the function with regEx exectuted. – JoakimB Aug 14 '15 at 18:08

0 Answers0