Question

I got a wierd issue with linkify'ing url to clickable links using javascript regular expression.

after digging a lot on so.com, i started using jmrware/LinkifyURL.js to convert url's to clickable links. but when try to convert url without schema ((htt|ft)ps), it is not recognising the url at all.

example:

http://www.google.com (accepting)
www.google.com(not accepting)
google.com(not accepting).

can anybody there help me to figure out solution for this.

Edit: in my scenario i have to accept input from user in text area where user may enter any url. i have to linkify any url found in user input while showing on site.

here is my test content I'm trying to linkify.

> http://example.com:80                                            (linkified)<br/>
> http://example.com:80/path/                                      (linkified)<br/>
> http://example.com:80/path/file.txt                              (linkified)<br/>
> http://example.com:80/path/file.txt?query=val&var2=val2          (linkified)<br/>
> http://example.com:80/path/file.txt?query=val&var2=val2#fragment (linkified)<br/>
> http://example.com/(file's_name.txt)                             (linkified)<br/>
> http://[2001:0db8:85a3:08d3:1319:8a2e:0370:7348]                 (linkified)<br/>
> http://[2001:0db8:85a3:08d3:1319:8a2e:0370:7348]/file.txt        (linkified)<br/>
> http://youtube.com:80/path/file.txt?query=val&var2=val2          (linkified)<br/>
> www.example.me                                                   (not linkified)<br/>
> example.me                                                       (not linkified)<br/>

in above code last two strings are not linkified which i want to be. I tried negative lookbehind in JavaScript RegExp, even then no luck.

((?!(http|ftp|https|ftps)://)^[a-z0-9-]+(.[a-z0-9-]+)+([/?].*)?) - negative lookbehind

I'm newbie to so.com and please excuse me if I'm not clear in my question.

Was it helpful?

Solution

I am the author of the linkify project in question. By design, the linkify() function only matches URLs which have the scheme element. Without this restriction, there would be too many false positives. (Almost any string or substring is a valid URI.)

OTHER TIPS

Add this line to ensure the url string is a link:

url = url.replace(/^(?!(?:http|https|ftp):\/\/)/i, 'http://');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top