Hi all, I need a function to get only the empty href query parameter names so I can replace them later with values from another array. After hours of failing at regular expressions, here is what i resorted to:
/**
* getEmptyQueryParams(URL)
* Input: URL with href params
* Returns an array containing all empty href query parameters.
*/
function getEmptyQueryParams(URL)
{
var params = new Array( );
var non_empty_params = new Array( );
var regex = /[\?&]([^=]+)=/g; // gets all query params
var regex2 = /[\?&]([a-zA-Z_]+)=[\w]/g; // gets non empty query params
while( ( results = regex.exec( URL ) ) != null )
{
params.push( results[1] );
}
while( ( results = regex2.exec( URL ) ) != null )
{
non_empty_params.push( results[1] );
}
while(non_empty_params.length > 0)
{
for(y=0;y < params.length;y++)
{
if(params[y] == non_empty_params[0])
{
params.splice(y,1);
}
}
non_empty_params.shift();
}
return params;
}
It works, but looks ugly as hell... Is there any better way to do it? Any help is appreciated.