var TLD = ['com','net','org','gov','edu','mil','biz','name','info','mobi','cn','hk']; var host = ''; function start(){ var regx = /(w+.){1,}w+/; var inpt = document.getElementById('inpt'); var txt = inpt.value.toLowerCase(); var findTLD = function(arr){ for(var j = 0;j<arr.length;j++){ if(TLD.indexOf(arr[j])!=-1){ return arr[j]; } } }; if(regx.test(inpt.value)){ var matches = regx.exec(inpt.value)[0]; var txtArr = matches.split('.'); host = txtArr[txtArr.indexOf(findTLD(txtArr))-1]; if(host){ inpt.value = matches.slice(matches.indexOf(host)); }else { alert('请输入正确的的URL'); } }else{ alert('请输入正确的的URL'); } }