What i try to do is when i have:
www.gmail,
www.gmail/,
,
,
,
,
www.gmail/example
just get gmail, 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 ). How can i improve this to match things that are of the form abcd.efgs ? I mean specify that the string should contain characters and at least one dot after the http,https,www. ?
Thanks in advance!
What i try to do is when i have:
www.gmail.,
www.gmail./,
http://www.gmail.,
https://www.gmail.,
http://gmail.,
https://gmail.,
www.gmail./example
just get gmail., 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 .). How can i improve this to match things that are of the form abcd.efgs. ? I mean specify that the string should contain characters and at least one dot after the http,https,www. ?
Thanks in advance!
Share Improve this question asked Oct 31, 2014 at 9:30 sstaurosssstauross 2,6782 gold badges32 silver badges51 bronze badges3 Answers
Reset to default 5If 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.
http://jsfiddle/q6z3xb6d/
[update]
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/q6z3xb6d/2/
^(?:https?:\/\/)?(?:www\.)?((?:(?!www\.|\.).)+\.[a-zA-Z0-9.]+)
Try this.See demo.
http://regex101./r/yG7zB9/7
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/6mrbbq9x/5/