jamcode 发表于 2013-1-29 08:47:43

JavaScript中匹配URL的正则表达式

网上流行着很多判断一个字符串是不是一个URL的JS代码。(或曰,判断用户输入是否是合法的URL)。

我发一个稍完整些的正则表达式,要Mark的赶紧MARK吧

var re=/^((http|https|ftp):\/\/)?(\w(\:\w)?@)?(+\.)*?(+\.{2,6}(\.{2})?(\:{2,6})?)((\/[^?#<>\/\\*":]*)+(\?[^#]*)?(#.*)?)?$/i;assert(re.test("www.jamcode.org"));assert(re.test("http://www.jamcode.org"));assert(re.test("http://www.jamcode.org/robots.txt"));assert(re.test("www.jamcode.org/robots.txt"));assert(re.test("https://www.jamcode.org/favicon.ico"));assert(re.test("http://www.jamcode.org/?search=Go"));//..............//..............



大家如果有更好的正则,赶紧地发出来

http://www.jamcode.org/
页: [1]
查看完整版本: JavaScript中匹配URL的正则表达式