slandau
slandau

Reputation: 24052

Javascript URL Regex issues

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?

Upvotes: 0

Views: 239

Answers (2)

Josh Leitzel
Josh Leitzel

Reputation: 15199

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.

Upvotes: 0

Nicole
Nicole

Reputation: 33197

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}/;

Upvotes: 1

Related Questions