场景实训 - 匹配URL

  • 作者:KK

  • 发表日期:2017.11.28


JS代码

var pattern = /^(http|https):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(?::\d{1,5})?(?:$|[?\/#])/i;

console.log( pattern.test('http://xxx.com') );	//true
console.log( pattern.test('http://xxx.com/yyy.html') );	//true
console.log( pattern.test('http://xxx.com/yyy.html?a=a1&b=b1#p1') );	//true

console.log( pattern.test('http://xxx') );	//false
console.log( pattern.test('http://xxx/yyy.html') );	//false
console.log( pattern.test('http:/xxx.com') );	//false
console.log( pattern.test('http:\\xxx.com') );	//false
console.log( pattern.test('http:xxx.com') );	//false
console.log( pattern.test('abc') );	//false
console.log( pattern.test('中文') );	//false

PHP代码

如果你只是想校验输入参数是否符合URL格式,推荐使用 filter_var 函数:

print_r([
	filter_var('http://xxx.com/a/b.html?a=a1#c', FILTER_VALIDATE_URL),	//有效,返回原文

	filter_var('http/xxx.com/a/b.html?a=a1#c', FILTER_VALIDATE_URL),	//无效,返回空
]);

如果想进行数据采集匹配才用正则吧:

$pattern = '@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@';

preg_match($pattern, 'http://xxx.com', $matchResult1);  //1
preg_match($pattern, 'http://xxx.com/yyy.html', $matchResult2);  // 1
preg_match($pattern, 'http://xxx.com/yyy.html?a=a1&b=b1#p1', $matchResult3);  // 1

preg_match($pattern, 'http://xxx', $matchResult4);  // 0
preg_match($pattern, 'http://xxx/yyy.html', $matchResult5);  // 0
preg_match($pattern, 'http:/xxx.com', $matchResult6);  // 0
preg_match($pattern, 'http:\\xxx.com', $matchResult7);  // 0
preg_match($pattern, 'http:xxx.com', $matchResult8);  // 0
preg_match($pattern, 'abc', $matchResult9);  // 0
preg_match($pattern, '中文', $matchResult10);  // 0

header('Content-type:text/plain');
print_r([
    $matchResult1,
    $matchResult2,
    $matchResult3,
    $matchResult4,   // 空
    $matchResult5,   // 空
    $matchResult6,   // 空
    $matchResult7,  // 空
    $matchResult8,  // 空
    $matchResult9,  // 空
    $matchResult10,  // 空
]);