Regex validation of an URL via a function

I am trying to run a method that validates an URL, and then, if the URL is Ok, Patrick’s Browser Bean will display it by calling to this function onShowDisplayWebpage (which does some other things).

This method or function is run as an ‘onShow’ event of a form.

function onShowHideShow(firstShow, event){
	var urlregexExpression = '(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=#]*)?';
	var urlregexValidation = metadata_initial_path_or_url.match(urlregexExpression);
		if(!urlregexValidation){
		elements.bean_browser.visible = false;
		}else{
		elements.bean_browser.visible = true;
		onShowDisplayWebpage();
		}	
}

I am sure it did not work because I am quoting the variable urlregexExpression. I need to escape some of the characters but I don’t know what I am doing wrong.

Has anyone validated an URL via a function before? Can you share the code?

Thanks! JC

To declare a (hardcoded) regular expression, you have to use slashes instead of quotes.
Then you also have to escape the literal slashes in the expression itself.

Try this:

var urlregexExpression = /(http|https):\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=#]*)?/;

I knew quotes would define the expression as a string. I just did it to save the function.

I was using the forward slash in everything! I guess I had to use the backward slash to scape a forward slash.

Joas: THANK YOU very much for your help.