0

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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it has some effect. Could you allow the url to have some common characters like "?" and "&", so (http://)somesite.com/?id=a&name=b is supported?
@mrmoment looks like I am late. Nevertheless, I have provided in my effort and edited my answer.
1

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

2 Comments

I add (https?:\/\/)? in the front to comply with the protocol given case. Still, I want to support url with parameters, e.g., some-site.com/?p=3&name=abc. I used /(https?:\/\/[^\s]+)/g. It is simple but not for protocol absent case.
@mrmoment I've updated the answer to support parameters. If you want to check for the existence of protocol you'll need to match if it exists and then replace accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.