View Full Version : Need help with parsing a URL list to get the host names

07-17-2011, 10:14 PM
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
I highly appreciate your help or direction.

07-17-2011, 11:33 PM
First of all you need transform a string presentation of a list to a real list (I mean an array):

var list = document.getElementById('list').value.split(/\s*,\s*/);

After that you need walk through each item and validate that it contains a hostname with no any part of URI:

This regular expression considers
these strings are valid

and others are invalid

var re = /^(?:\w+:\/\/)?([^\/]+)\/?$/;

Now you need assemble all parts above with the following code and put the actual modifications:

for (var i = 0; i < list.length; i++) {
var m = list[i].match(re);
if ( ! m ) {
// FAIL!!! Inform a user about invalid input
// OK!!! Do something with a valid entry
// m[1] contains a hostname only