0

I try this regex: ^(?:https?://)?(?:www.)?((?:(?!www.|.).)+.[a-zA-Z0-9.]+) in the validator here: regexplanet.com and the results are fine for the inputs provided but it fails in this fiddle:jsfiddle

<input type="text" id="domain">

$(document).ready(function(){
 $('#domain').blur(function(){
 var str = $.trim($(this).val());
 var pat = /^(?:https?:\/\/)?(?:www\.)?((?:(?!www\.|\.).)+\.[a-zA-Z0-9.]+)/mg;

 if (pat.test(str)){
    var match = str.match(pat);
    $(this).val(match);
 }
 else{
    $(this).val('Validation failed');
 }
});

What i try to do is to give: www.gmail.com, http://www.gmail.com, gmail.com/example etc and just get gmail.com

sstauross
  • 2,602
  • 2
  • 30
  • 50
  • I think it's a problem with `.blur` event handling either in jsfiddle, jquery or Chrome. Your code works if you use `.change` instead of `.blur` (and it's the second question I've seen today where blur doesn't work as expected). – pawel Oct 31 '14 at 10:59
  • what is your expected output? – nu11p01n73R Oct 31 '14 at 11:01
  • works fine for me, in latest chrome. – Ravi Oct 31 '14 at 11:03
  • I think (s)he wants just the domain without the www or http – Jared Smith Oct 31 '14 at 11:04
  • the expected output is anything after the http://,https://,www. e.g. when you put www.gmail.com => gmail.com, http://www.gmail.com/example => gmail.com etc.. – sstauross Oct 31 '14 at 11:04
  • @Ravi can confirm, `.blur` works as expected after restarting Chrome post-update. – pawel Oct 31 '14 at 11:10
  • Your fiddle works fine the way it is coded. You are not actually extracting the domain alone. It justs checks the pattern and if matches the same will be displayed in the text box. You have to parse the Uri to extract the individual parts. Please take a look at this [post](http://stackoverflow.com/questions/8498592/extract-root-domain-name-from-string) – Thangadurai Oct 31 '14 at 11:32
  • ......................:) – vks Oct 31 '14 at 11:54
  • Its not a blur problem, @Thangadurai how could i make this return the expected output? For example this:http://jsfiddle.net/6mrbbq9x/1/ does partially what i want except that i dont want to match e.g. the word gmail alone but also say that after www.,http,https would be something that would include characters and dots. So? – sstauross Oct 31 '14 at 12:13
  • Take a look at this JS library, which can be used for your requirement. [http://medialize.github.io/URI.js/](http://medialize.github.io/URI.js/) – Thangadurai Oct 31 '14 at 13:18

3 Answers3

0

This does what you've asked for so far:

var tests = [
    'www.gmail.com',
    'http://www.gmail.com',
    'https://www.gmail.com',
    'https://gmail.com/',
    'www.gmail.com/asdf',
    'gmail.com/',
    'gmail.com/example'
];

var domains = tests.map(function(url){
    return url.replace(/^(https?:\/\/)?(www\.)?|\/.*/g, '');
})

domains.every(function(domain){
    return domain === 'gmail.com';
}); // true

You might want to edit your question to better specify your inputs and outputs. And some examples would be good.

1983
  • 5,882
  • 2
  • 27
  • 39
  • I just look for a pattern to get rid of www., http://, https:// and get the rest except from a possible slash (/) e.g. from www.gmail.com/example i would like gmail.com only, if i had gmail.com.gr.whatever/test i would like gmail.com.gr.whatever. Is it clear? This fiddle: http://jsfiddle.net/6mrbbq9x/3/ does what i want except that it also accepts single words like gmail it leaves it as it is.. i dont want that too...i would be glad if you could provide a solution to that – sstauross Oct 31 '14 at 12:40
0

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/

sstauross
  • 2,602
  • 2
  • 30
  • 50
0

As per the comment i believe you want to replace

'www.gmail.com',
'http://www.gmail.com',
'https://www.gmail.com',
'https://gmail.com/',
'www.gmail.com/asdf',
'gmail.com/',
'gmail.com/example' with gmail.com only 

So here is the regex:

var pat = /^(https?:\/\/)?(?:www\.)?([^\/]+).*$/

See also this jsfiddle: http://jsfiddle.net/6mrbbq9x/3/

in this jsfiddle i have replaced .blur with .change

  • yes but i don't want to match a word which has no .tld for example gmail(without .com) if you put it with your current solution gmail stays in the input, i want to show validation failed in this case! Thanks a lot for your answer though.. – sstauross Nov 03 '14 at 12:20