SandBox
SandBox

Reputation: 165

Javascript to extract *.com

I am looking for a javascript function/regex to extract *.com from a URI... (to be done on client side)

It should work for the following cases:

siphone.com = siphone.com
qwr.siphone.com = siphone.com
www.qwr.siphone.com = siphone.com
qw.rock.siphone.com = siphone.com
<http://www.qwr.siphone.com> = siphone.com

Much appreciated!

Edit: Sorry, I missed a case:

http://www.qwr.siphone.com/default.htm = siphone.com

Upvotes: 0

Views: 176

Answers (6)

andsens
andsens

Reputation: 6948

Use regexp to do that. This way modifications to the detections are quite easy.

var url = 'www.siphone.com';
var domain = url.match(/[^.]\.com/i)[0];

If you use url.match(/(([^.]+)\.com)[^a-z]/i)[1] instead. You can assure that the ".com" is not followed by any other characters.

Upvotes: 0

Scherbius.com
Scherbius.com

Reputation: 3414

var myStrings = [
  'siphone.com',
  'qwr.siphone.com', 
  'www.qwr.siphone.com', 
  'qw.rock.siphone.com', 
  '<http://www.qwr.siphone.com>'
  ];

for (var i = 0; i < myStrings.length; i++) {
    document.write( myStrings[i] + '=' +  myStrings[i].match(/[\w]+\.(com)/gi) + '<br><br>');
}

I've placed given demo strings to the myStrings array.
i - is index to iterate through this array. The following line does the matching trick:

myStrings[i].match(/[\w]+\.(com)/gi)

and returns the value of siphone.com. If you'd like to match .net and etc. - add (com|net|other) instead of just (com).

Also you may find the following link useful: Regular expressions Cheat Sheet

update: missed case works too %)

Upvotes: 1

ellisbben
ellisbben

Reputation: 6371

This should do it. I added a few cases for some nonmatches.

var cases = [
  "siphone.com",
  "qwr.siphone.com",
  "www.qwr.siphone.com",
  "qw.rock.siphone.com",
  "<http://www.qwr.siphone.com>",
  "hamstar.corm",
  "cheese.net",
  "bro.at.me.come",
  "http://www.qwr.siphone.com/default.htm"];

var grabCom = function(str) {
  var result = str.match("(\\w+\\.com)\\W?|$");
  if(result !== null)
    return result[1];
  return null;
};

for(var i = 0; i < cases.length; i++) {
  console.log(grabCom(cases[i]));
}

Upvotes: 1

Oliver Nightingale
Oliver Nightingale

Reputation: 1835

uri = "foo.bar.baz.com"
uri.split(".").slice(-2).join(".") // returns baz.com

This assumes that you want just the hostname and tld. It also assumes that there is no path information either.

Updated now that you also need to handle uris with paths you could do:

uri.split(".").slice(-2).join(".").split("/")[0]

Upvotes: 0

Mateen Ulhaq
Mateen Ulhaq

Reputation: 27201

I guess this regex should work for a few cases:

/[\w]+\.(com|ca|org|net)/

I'm not good with JavaScript, but there should be a library for splitting URIs out there, right?

According to that link, here's a "strict" regex:

/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/

As you can see, you're better off just using the "library". :)

Upvotes: 1

secretformula
secretformula

Reputation: 6432

You could split the string then search for the .com string like so

var url = 'music.google.com'
var parts = url.split('.');
for(part in parts) {
    if(part == 'com') {
        return true;
    }
{

Upvotes: 0

Related Questions