1

I have a Javascript function that receives a string. that string may have URLs in it, with, or without http:// or http/s:

example string:

This is an example string with www.cnn.com and http://microsoft.com"

my purpose is to take that string and inject anchor tags for the URLs such that they are clickable when injected as HTML to an html document.

Is there anything I can use that does this? some Jquery function?

sQVe
  • 1,977
  • 12
  • 16
JasonGenX
  • 4,952
  • 27
  • 106
  • 198

2 Answers2

2

No native jQuery function for this but regular expression can do this

Let me give point you to this blog post that will get you started. What it has is a regular expression (may seem rather complicated and complex, but is not that much) that can detect links and emails in strings so you'll be able to find them and replace them with links that point to extracted URLs.

Maybe an even better expression (that I know I used in the past) has been published by Stackoverflow's own Jeff Atwood. He also describes the whole URL situation in very much detail so you can decide whether this applies to your problem and how much or not.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • 1
    yep. that did the trick. I'm not a web developer and never used Javascript before. I use QWebView (Qt webKit) I had an urgent need to get something like that done. Thanks for your help. – JasonGenX Aug 25 '12 at 04:03
0

If a regex still is wanted:

/((www|http:\/\/)\S*)/ig


This matches www.cnn.com and http://www.microsoft.com from the following example:

This is an example string with www.cnn.com and http://www.microsoft.com

If you use <a href="$1">$1</a> to replace you'll get:

<a href="www.cnn.com">www.cnn.com</a>
<a href="http://www.microsoft.com">http://www.microsoft.com</a>


Remember that this regex doesn't validate the urls address. It just searches for www or http:// and then takes everything after that until there is a whitespace.

sQVe
  • 1,977
  • 12
  • 16
  • That's not really good, because take this string for example that would generate invalid link: `Find it on my blog (http://somewhere.com).`. This will then capture closing parentheses and dot as well... That's undesired. And what if there's no white space? What if link is the last thing in the string? Things are usually not as simple as they seem. – Robert Koritnik Aug 24 '12 at 22:21
  • @RobertKoritnik I never said that it was any good, there are already tons of dupes. I was just trying to explain how one could go about to do the thing. I even state in the end that it doesn't validate URLs. – sQVe Aug 24 '12 at 22:25
  • You're right. but my example has nothing to do with URL validation. My example just showed that it doesn't extract URL properly, because in some cases it extracts too much which is not part of a URL any more. – Robert Koritnik Aug 24 '12 at 22:26
  • @RobertKoritnik It does, there are a **ton** of different chars that an URL can end with. If you want a better match use: `(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])` and even that isn't 100%. [Read more here](http://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links) – sQVe Aug 24 '12 at 22:29