I'm trying to grab the TLD from an input form. Having a hard time. Here are example inputs. http://www.test.com http://test.com test.com http://www.test.com/ http://www.test.com/index.php http://www.test.com/index.php?action=x I need the regex to return only "test.com" Basically code has to look like this I guess: theinput = document.getElementById('domain_input').value; var regexp = /([^.]+)(\..*)/; var match = regexp.exec(theinput); var prefix = match[1]; var domain = match[2]; var extra = match[3]; alert ( domain ) My issue I believe is the regexp line. I can't seem to get this working at all. Any help is appreciated. If you get me perfect working code I'll Paypal you $5. Thanks.
function parseUri(sourceUri){ var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"]; var uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)?((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri); var uri = {}; for(var i = 0; i < 10; i++){ uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : ""); } if(uri.directoryPath.length > 0){ uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/"); } domain = uri['domain']; onlyhost = domain.replace('www.', ''); return onlyhost; } alert(parseUri('http://www.test.com/index.php?action=x')); HTML:
Does it have to be a regex? An easier method is to take out the parts one by one: <html> <head> <style type="text/css"> p { float: left; font-size: 18px; margin: 0; width: 400px; } div { clear: both; } </style> <script type="text/javascript"> function getDomain(s) { var i = s.indexOf("://"); if (i != -1) s = s.slice(i + 3, s.length); i = s.indexOf("www."); if (i != -1) s = s.slice(i + 4, s.length); i = s.indexOf(":"); if (i != -1) s = s.slice(0, i); i = s.indexOf("/"); if (i != -1) s = s.slice(0, i); return s; } var test = ["http://www.test.com", "http://test.com", "test.com", "http://www.test.com:8080/", "ftp://www.test.com/index.php", "https://www.test.com/index.php?action=x", "http://test.co.uk/", "http://www.test.co.uk/", "http://subdomain.test.com/"]; for (i=0; i<test.length; i++) { var s = test[i]; document.write("<div><p>"+s+"</p><p>"+getDomain(s)+"</p></div>"); } </script> </head> <body> </body> </html> Code (markup):