开发者

Check if an URL is a valid Google search URL

I'd like to check if an URL is a valid Google search URL

(e.g. http://www.google.com/#sclient=psy&hl=en&site=&source=hp&q=car&aq=f&aqi=&开发者_开发技巧aql=&oq=&pbx=1&bav=on.2,or.r_gc.r_pw.&fp=8f6073eaa2ffaabc&biw=1280&bih=868 is a valid URL for a Google search for the word "car" whereas http://www.google.com/#sclient=psy&hl=en&site=&sourciw=1280&bih=868&fp=1&bav=on.2,or.r_gc.r_pw.&cad=b isn't [it displays a blank page])

Does someone know of a way to do this ? (regex, validator, ...)


Just check for the presence of the q parameter:

/[?&]q=[^&]+/

Wrap in parenthesis if you need to extract the search term:

/[?&]q=([^&]+)/

Demo


EDIT
Actually, the correct regex is this, since the "query string" is in the hash of the URL:

/[#&]q=([^&]+)/

Demo

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜