0

I use this regex for URL's:

var re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;

However, it seems to only be matching URL's that are alone. If you add a URL to an already typed message, it won't match:

Test message: www.google.com

Won't match. How do I make it match URL's no matter what's included with them?

2
  • {0,1} should be ?. Further you should keep international domains and long top-level domains like .berlin in mind. Commented Jul 14, 2011 at 2:36
  • -1: If you're going to use regular expressions you should at least have some clue how they work. At least the basics. Commented Jul 14, 2011 at 2:38

2 Answers 2

1

It's because the ^ character at the beginning of the expression means "match the beginning of the string" (in other words, what you have is a starts-with search). Remove that and it will match anywhere in the test string.

var re = /(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
Sign up to request clarification or add additional context in comments.

Comments

0

That's simply because you're using the caret symbol (^). That means "only match the start of input." Remove that and you should get what you want.

Comments

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.