gpt4 book ai didi

objective-c - Chrome 地址栏如何确定它是 URL 还是搜索字符串?

转载 作者:搜寻专家 更新时间:2023-10-30 20:17:28 25 4
gpt4 key购买 nike

- (BOOL) validateUrl: (NSString *) candidate {
NSString *urlRegEx =
@"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
if( [urlTest evaluateWithObject:candidate]
||[candidate containsString:@".com"]
||[candidate containsString:@".net"]
||[candidate containsString:@".org"]
||[candidate containsString:@".cn"]
||[candidate containsString:@".jp"]
)
{
return TRUE;
}

return FALSE;
}

这是一长串URL域名,“.com”、“.net”、“.org”等等。人们不需要在前面或地址栏中输入“http”。

那么 Chrome 地址栏如何确定它是一个 URL 还是一个搜索字符串呢?

If I input "a.fa", it's not an URL.
"a a.com",it's a search string.
"a.mobi/aaa", it's an URL.

最佳答案

正如 funroll 提到的那样,通过 Chromium 找到答案是可能的——但至少根据我的测试,这是正在发生的事情的基本思路。

如果输入到“多功能框”的字符串遵循以下格式,则该字符串被确定为 URL:

[协议(protocol)][子域].[子域].[域名].[tld]

其中子域(当然是可选的)和域名都只包含字母(对于 Chrome,这似乎包括重音字母)、数字、空格和连字符,并且 TLD/顶级域来自批准列表—.com、.net 等—除非指定了协议(protocol),在这种情况下,任何 TLD 都被视为有效。协议(protocol)也来自一个集合列表,但可以是几乎任何格式,冒号跟在任意数量的斜杠之后。如果协议(protocol)不是设置列表的一部分,则整个 URL 将被视为搜索。

如果上述 URL 格式(例如 stackoverflow.com/)中的字符串后有斜杠,则后面的任何内容都有效。

或者,如果斜杠出现在字符串的开头,Chrome 也将其视为一个 URL(使用 file:// 协议(protocol))。


有效 URL 示例(根据 Chrome):

  • stackoverflow.com
  • abc.stackoverflow.com
  • abc.abc.abc.abc.stackoverflow.com
  • stáckoverflow.com (this changes the URL, but is allowed—try it!)
  • stack-overflow.com
  • -stackoverflow.com (might not even be a legal domain name, but it works)
  • 4stackoverflow.com
  • stackoverflow.com
  • stackoverflow.com/not valid characters !@#$^æ
  • [http]://stackoverflow.com (the brackets aren't legal, but I can't include the link otherwise)
  • [http]:////stackoverflow.com
  • [http]:stackoverflow.com
  • [http]:stackoverflow.mynewtld

无效 URL 示例:

  • stack overflow.com
  • stackoverflow*.com
  • stack/overflow.com
  • stackoverflow.mynewtld

And, well, just about everything else.


我们只希望在某处有一个图书馆来做这一切。

关于objective-c - Chrome 地址栏如何确定它是 URL 还是搜索字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24765294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com