Need help with parsing a URL list to get the host names

Hello All,

I have a form where I ask my users for a comma seperated web site list.
I want to make sure this list only contains the host names of URLs without any white spaces

Original list: site1.com, http://site2.com, http://www.site3.com,sub.site4.com
Edited list: site1.com,site2.com,site3.com,sub.site4.com

Currently I have the following to get the comma separated list

var list = document.getElementById( "list" ).value ;

Can some one please help me with above task of getting host names and trimming for white spaces?
I am very new to JavaScript language and highly appreciate your help or direction.

You can use the following code, which will remove all whitespace from the list:


var list = document.getElementById("list").value.replace(/\\s/g, "");

The replace() function is a String function that takes a regular expression to search for, and then a replacement string. The regular expression /\s/ looks for any kind of whitespace, and the g modifier on the end instructs the replace function to make the replacement throughout the entire string, and not to just stop after the first match. The second argument to the function, the empty string (“”) tells it to replace all whitespace it finds with nothing, effectively removing all whitespace from the string.

I have found some sample codes at

It explains how to get the host from a given single URL and I can use bhawk90’s for white space.

However, I am still not sure how to do the host name conversion for the whole list. Need to do some iteration but no knowledge regarding that.


// This function creates a new anchor element and uses location
// properties (inherent) to get the desired URL data. Some String
// operations are used (to normalize results across browsers).
 
function parseURL(url) {
    var a =  document.createElement('a');
    a.href = url;
    return {
        source: url,
        protocol: a.protocol.replace(':',''),
        host: a.hostname,
        port: a.port,
        query: a.search,
        params: (function(){
            var ret = {},
                seg = a.search.replace(/^\\?/,'').split('&'),
                len = seg.length, i = 0, s;
            for (;i<len;i++) {
                if (!seg[i]) { continue; }
                s = seg[i].split('=');
                ret[s[0]] = s[1];
            }
            return ret;
        })(),
        file: (a.pathname.match(/\\/([^\\/?#]+)$/i) || [,''])[1],
        hash: a.hash.replace('#',''),
        path: a.pathname.replace(/^([^\\/])/,'/$1'),
        relative: (a.href.match(/tps?:\\/\\/[^\\/]+(.+)/) || [,''])[1],
        segments: a.pathname.replace(/^\\//,'').split('/')
    };
}

This need to be iterated (I think)

var myURL = parseURL(single_URL Here)

Please help if you know how to do this. Greatly appreciate it.

Something like this should do it:


var list = document.getElementById("list").value.replace(/\\s/g, "");
var urls = list.split(",");
for (i in urls) {
parsed_url = parseURL(urls[i]);
// Do something with parsed_url
}

After removing all whitespace, the variable urls is set to an array of the submitted urls, which is created by telling the split function which character should be used as a separator, which in this case is a comma. Then, using a for…in loop, you loop through all the urls in the urls array and call the parseURL function on them, and do what you need to do with the parse_url variable.