1

What i try to do is when i have:

www.gmail.com,

www.gmail.com/,

http://www.gmail.com,

https://www.gmail.com,

http://gmail.com,

https://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!

3 Answers 3

5

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.

http://jsfiddle.net/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.net/q6z3xb6d/2/

Sign up to request clarification or add additional context in comments.

1 Comment

your solution seems to work great! Thats what i expected.. Thanks a lot! i mark it as the correct answer to the original question.
0
^(?:https?:\/\/)?(?:www\.)?((?:(?!www\.|\.).)+\.[a-zA-Z0-9.]+)

Try this.See demo.

http://regex101.com/r/yG7zB9/7

8 Comments

its a good answer although when you put something like: www.gmail.example.com will return only gmail.example despite i would like to return the whole thing after www. could you edit it to take this into consideration?
Perfect man! I would appreciate if you could explain to me where in this regex do you exclude the slash(/)? Edit your answer with the correct regex and i take it as accepted!:)
@sstauross i am not actually excluding /.It just that [a-zA-Z0-9.] character class does not contain / so it cannot be matched.It will matchuntill it encounter a character out of the defined class
Look at this fiddle: jsfiddle.net/6mrbbq9x when i try the regex does not actually return what i want, what am i doing wrong?
Is there another way to get that? Because this: jsfiddle.net/6mrbbq9x/1 seems to partially work...
|
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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.