这是Prestaul答案的更新版本,它解决了我在评论中提到的两个问题.
var GOOGLE_DOMAINS = ([ '.google.com', '.google.ad', '.google.ae', '.google.com.af', '.google.com.ag', '.google.com.ai', '.google.am', '.google.it.ao', '.google.com.ar', '.google.as', '.google.at', '.google.com.au', '.google.az', '.google.ba', '.google.com.bd' ]).join('\n'); function isGoogleUrl(url) { // get the 2nd level domain from the url var domain = /^https?:\/\/[^\///]*(google\.[^\/\\]+)\//i.exec(url); if(!domain) return false; domain = '.'+domain[1]; // create a regex to check to see if the domain is supported var re = new RegExp('^' + domain.replace(/\./g, '\\.') + '$', 'mi'); return re.test(GOOGLE_DOMAINS); } alert(isGoogleUrl('http://www.google.ba/the/page.html')); // true alert(isGoogleUrl('http://some_mal_site.com/http://www.google.ba/')); // false alert(isGoogleUrl('https://google.com.au/')); // true alert(isGoogleUrl('http://www.google.com.some_mal_site.com/')); // false alert(isGoogleUrl('http://yahoo.com/')); // false