mrmoment
mrmoment

Reputation: 757

Detect protocol optional url in text with Javascript

I found a lot of answers to detect url in text. I tried them but failed to detect protocol optional urls. Most of existing solutions could find urls in "Hello, open http://somesite.com/etc to see my photo" and replace the http part to tags. But they could not work for cases like "Hey, open somesite.com or somesite.com/etc to take a look."

How can I detect both cases (better with one regex), and the default protocol is "http://" for none-protocol urls.

I note that SO also failed to detect the latter case...

Edit: Maybe it is error-prone to change somesite.com into urls (in English), so this requirement is not ideal?

I used regex /(https?://[^\s]+)/g. This one is simple and support cases like http://abcd.com/etc/?v=3&type=xyz. But I don't know how to change it to support protocol absent case.

Upvotes: 0

Views: 200

Answers (2)

Gaurang Tandon
Gaurang Tandon

Reputation: 6753

You can use this:

^(https?:\/\/)?\w+\.\w+([\/\w\?\-\+\!\&\$\=\.\:]+)*$

Matches:

www.domain.com
http://www.domain.com
http://www.somesite.com/etc
http://www.somesite.com/etc/etc/etc
somesite.com
somesite.com/etc

http://google.com/q=regex+for+this&utm-source=stackoverflow

DEMO

Upvotes: 1

Pedro Lobito
Pedro Lobito

Reputation: 98901

This isn't bullet proof, but it works with the example you've provided:

var text = "Hey, open some-site.com or somesite.com/etc?test=1 to take a look."
var myregexp = /([\-A-Z0-9+&@#\/%=~_|]+\.[a-z]{2,7})(\/[\-A-Z0-9+&@#\/%=~_|?]+)?/ig;
var result = text.replace(myregexp, '<a href="http://$1$2">$1$2</a>');
alert(result);

//Hey, open <a href="http://some-site.com">some-site.com</a> or <a href="http://somesite.com/etc?test=1">somesite.com/etc?test=1</a> to take a look.

LIVE DEMO

Upvotes: 1

Related Questions