Implementing an "omnibar" like Google Chrome's... need method to determine query or URL
Before I do this myself I thought I'd ask here and see if there are any quick solutions.
I don't need the history-lookup, autocomplete, or other fancy features of Chrome's omnibar; I just need to detect if a string looks like a valid URL (with valid TLDs and scheme/protocol) or ip-address (IPV4 and IPV6).
I'm not sure how to search chrome's code repository for a look at how they do it. So if you can find that for me that would be an acceptable answer (bonus points if you port it to javascript!).
Tagged as language-agnostic because I don't care what language it is in [well, please no Ook or开发者_C百科 LOLCODE]). Javascript will be the final implementation, though.
There's a regex provided in RFC2396 - Uniform Resource Identifiers (URI): Generic Syntax (see appendix B) :
The following line is the regular expression for breaking-down a URI
reference into its components.^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? 12 3 4 5 6 7 8 9
The numbers in the second line above are only to assist readability; they indicate the reference points for each subexpression (i.e., each
paired parenthesis). We refer to the value matched for subexpression as $. For example, matching the above expression tohttp://www.ics.uci.edu/pub/ietf/uri/#Related
results in the following subexpression matches:
$1 = http: $2 = http $3 = //www.ics.uci.edu $4 = www.ics.uci.edu $5 = /pub/ietf/uri/ $6 = <undefined> $7 = <undefined> $8 = #Related $9 = Related
I don't see a reason to not use regex in this case, especially if you're just checking if it's a URI or if it's a search.
精彩评论