开发者

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 se开发者_运维问答ems 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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜