0

I want to extract the domain name from my - {{video.url}}

The {{video.url}} contains different links like this:

https://domain1.com/123/abc
https://www.domain2.com/123/abc
http://www.domain3.com/123/abc
http://domain4.com/123/abc
https://www.domain5.com?123&abc
https://www.domain6.com/123/abc.html

I need the "domain1" "domain2" "domain3" ... and use this to create an image name under my component.ts:

return'https://mywebsite.com/img/'+domain+'.png';

The folder "/img/" will have many images named, youtube.png - google.png ...

On my angular component.html I want to add

<div class="video" *ngFor="let video of mediaItem.videos">
<img [src]="getIcon">
</div>

But what code should I use in component.ts ???

ivan
  • 95
  • 3
  • 14

1 Answers1

0

const getHost = host => {
  const hostArr = host.toLowerCase().replace(/https?:\/\//, '').split('.');
  return hostArr[0] === 'www' ? hostArr[1] : hostArr[0];
}

console.log(getHost('https://domain1.com/123/abc'));
console.log(getHost('https://www.domain2.com/123/abc'));
console.log(getHost('http://www.domain3.com/123/abc'));
console.log(getHost('http://domain4.com/123/abc'));
console.log(getHost('https://www.domain5.com?123&abc'));
console.log(getHost('https://www.domain6.com/123/abc.html'));
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60
  • An complete code to copy/paste under my component.ts please??? I need to extract the hostname from `video.url` – ivan Mar 13 '19 at 03:22
  • It's not what I'm looking for, I want to extract the hostname directly from `video.url` This is an example of other funccion used under component.ts , in this case to open on new window the `video.url` -this- `public playVideo(video: Video) { if (video.type === 'external') { window.open(video.url, '_blank'); } else { this.store.dispatch(new PlayVideo(video, this.mediaItem)); } }` – ivan Mar 13 '19 at 03:36
  • You can write me a code based on this example, in which the `return` will be: `return'https://mywebsite.com/img/'+domain+'.png';` - and the `'+domain+` is the hostname??? - Many thanks. – ivan Mar 13 '19 at 03:39