Regular expression for checking website url
This is a pretty basic expression for testing domain names:
@^(http\:\/\/|https\:\/\/)?([a-z0-9][a-z0-9\-]*\.)+[a-z0-9][a-z0-9\-]*$@i
Should match:
domain.com
www.domain.com
http://domain.com
https://domain.com
Regex for website or url validation
Use the regex ^((https?|ftp|smtp):\/\/)?(www.)?[a-z0-9]+\.[a-z]+(\/[a-zA-Z0-9#]+\/?)*$
This is a basic one I build just now. A google search can give you more.
Here
- ^ Should start with
- ((https?|ftp|smtp)://)? may or maynot contain any of these protocols
- (www.)? may or may not have www.
- [a-z0-9]+(.[a-z]+) url and domain and also subdomain if any upto 2 levels
- (/[a-zA-Z0-9#]+/?)*/? can contain path to files but not necessary. last may contain a
/
- $ should end there
var a=["http://www.sample.com","https://www.sample.com/","https://www.sample.com#","http://www.sample.com/xyz","http://www.sample.com/#xyz","www.sample.com","www.sample.com/xyz/#/xyz","sample.com","sample.com?name=foo","http://www.sample.com#xyz","http://www.sample.c"];var re=/^((https?|ftp|smtp):\/\/)?(www.)?[a-z0-9]+(\.[a-z]{2,}){1,3}(#?\/?[a-zA-Z0-9#]+)*\/?(\?[a-zA-Z0-9-_]+=[a-zA-Z0-9-%]+&?)?$/;a.map(x=>console.log(x+" => "+re.test(x)));
Regex for validate a website URL
(:?(?:https?:\/\/)?(?:www\.)?)?[-a-z0-9]+\.(?:com|gov|org|net|edu|biz)
https://regex101.com/r/bH7eL5/2
Regular expression for URL validation (in JavaScript)
The actual URL syntax is pretty complicated and not easy to represent in regex. Most of the simple-looking regexes out there will give many false negatives as well as false positives. See for amusement these efforts but even the end result is not good.
Plus these days you would generally want to allow IRI as well as old-school URI, so we can link to valid addresses like:
http://en.wikipedia.org/wiki/Þ
http://例え.テスト/
I would go only for simple checks: does it start with a known-good method: name? Is it free of spaces and double-quotes? If so then hell, it's probably good enough.
Regular expression to match URLs in Java
Try the following regex string instead. Your test was probably done in a case-sensitive manner. I have added the lowercase alphas as well as a proper string beginning placeholder.
String regex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
This works too:
String regex = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
Note:
String regex = "<\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]>"; // matches <http://google.com>
String regex = "<^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]>"; // does not match <http://google.com>
Javascript: Website url validation with regex
Even if this answer is a bit too much for this Problem, it illustrates the problem: Even if it might be possible to create a regexp to check the url, it is much simpler and more robust to parse the URL and "create a real Object", on/with which the overall test can be decomposed to a number of smaller tests.
So probably the builtin URL
constructor of modern browsers may help you here (LINK 1, LINK 2).
One approach to test you url might look like this:
function testURL (urlstring) {
var errors = [];
try {
var url = new URL(urlstring);
if (!/https/.test(url.protocol)) {
errors.push('wrong protocol');
}
//more tests here
} catch(err) {
//something went really wrong
//log the error here
} finally {
return errors;
}
}
if (testURL('mr.bean').length == 0) { runSomething(); }
URL Validation REGEX - URL just valid with http://
Remove the |www\.
?
^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
Related Topics
PHP Warning: PHP Startup: Unable to Load Dynamic Library 'Pdo_Mysql.So'
Ignoring Errors in File_Get_Contents Http Wrapper
If Statement Within an Array Declaration ...Is That Possible
Read the Longest String from an Array in PHP 5.3
How to Get the Last Dir from a Path in a String
Table Name as Parameter Using Pdo/MySQL Prepared Statement
What Is the Easiest Way to Use the Head Command of Http in PHP
How to Make PHP Curl Request with Port Number
How to Convert an Iso8601 Date to Another Format in PHP
Discount for Certain Category Based on Total Number of Products
Strip HTML Tags and Its Contents
PHP - Fastest Way to Check Presence of Text in Many Domains (Above 1000)
How to Get the Current PHP Executable from Within a Script
PHP Variable Variables with Array Key
How to Attach an Image Using PHPmailer