Skip to content

URL Regex JavaScript | Match HTTP/HTTPS Protocol code

  • by

Checking whether a single string is itself a valid URL or not.

URL Regex to ensure URL starts with HTTP/HTTPS:

https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)

Not require HTTP protocol

[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)

URL Regex JavaScript Examples

HTML example code:

<html>
<body>
	<script>

		var expression = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/gi;
		
		var regex = new RegExp(expression);
		var tURL = 'www.eyehunts.com';

		if (tURL.match(regex)) {
			alert("Successful match");
		} else {
			alert("No match");
		}	
	</script>
</body>
</html>

Output:

URL Regex JavaScript

Do comment if you have any doubts or suggestions on this JS URL topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading