Reputation: 2680
What i try to do is when i have:
www.gmail.com,
www.gmail.com/,
www.gmail.com/example
just get gmail.com, by far from searching into relative questions i have pattern to match these things which is:
var pat = /^(https?:\/\/)?(?:www\.)?([^\/]+)/;
but it also returns true when i put also a word eg. gmail (without .com). How can i improve this to match things that are of the form abcd.efgs.com ? I mean specify that the string should contain characters and at least one dot after the http,https,www. ?
Thanks in advance!
Upvotes: 1
Views: 6198
Reputation: 2691
If my understanding is right, you want to match only the domain names of a URL.
You can do this with this pattern
(?:\w+\.)+\w+
I have copied your JavaScript Fiddle and made changes to demonstrate this using html text and textarea boxes. The textbox demo, extracts the domain name from a user entered URL. The textarea box demo lists all the domains in the entered multiline text.
Just read your question again. Looks like you want to exclude matches for domains beginning with www
. You can use this pattern for that:
(?!(w+)\.)\w*(?:\w+\.)+\w+
JS fiddle demo - Updated version:
http://jsfiddle.net/q6z3xb6d/2/
Upvotes: 5
Reputation: 2680
Given an input field like:
<input type='text' id='domain'/>
i ended up with this solution in which i first validate it as a url and then get the string without 'http://', 'https://' , 'www.'
$(document).ready(function(){
function ValidUrl(str) {
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
if(!pattern.test(str)) {
return false;
} else {
return true;
}
}
$('#domain').change(function(){
var str = $.trim($(this).val());
if(ValidUrl(str)){
var pat = /^(https?:\/\/)?(?:www\.)?([^\/]+)/;
var match = str.match(pat);
console.log(match);
//$(this).val(str);
}
else{
$(this).val('Validation failed');
}
});
});
See also this jsfiddle:http://jsfiddle.net/6mrbbq9x/5/
Upvotes: 0
Reputation: 67968
^(?:https?:\/\/)?(?:www\.)?((?:(?!www\.|\.).)+\.[a-zA-Z0-9.]+)
Try this.See demo.
http://regex101.com/r/yG7zB9/7
Upvotes: 0