user884899
user884899

Reputation: 81

How to create URL links from strings

I'm using:

var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return text.replace(exp,"<a href='$1' target='internet'>$1</a>"); 

to turn, for example http://www.example.com into a clickable link. This works well!

Does anyone know how to expand this expression to capture also www.example.com (i.e without http://)?

Upvotes: 3

Views: 226

Answers (2)

Jason Gennaro
Jason Gennaro

Reputation: 34855

This worked for me:

Add a ( )* here

((https?|ftp|file):\/\/)*

http://jsfiddle.net/jasongennaro/NBWyr/

Basically zero or more of the items in ()

Upvotes: 1

c0deNinja
c0deNinja

Reputation: 3986

try this regex:

(?<http>(http:[/][/]|www.)([a-z]|[A-Z]|[0-9]|[/.]|[~])*)

Upvotes: 0

Related Questions