Retrieving host name in Angular 7 to dynamically generate image names

I am looking for a way to retrieve the domain name from my video URL: {{video.url}}

The URL {{video.url}} contains various links such as:

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 want to extract "domain1," "domain2," "domain3," and so on, in order to create an image name within my component.ts file:

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

The "/img/" directory will contain numerous images with names like youtube.png, google.png, and more.

In my angular component.html file, I intend to include:

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

Now, my question is what code do I need to use in my component.ts file?

Answer №1

function extractDomain(url) {
  const domain = url.toLowerCase().replace(/https?:\/\//, '').split('.');
  return domain[0] === 'www' ? domain[1] : domain[0];
}

console.log(extractDomain('https://domain1.com/123/abc'));
console.log(extractDomain('https://www.domain2.com/123/abc'));
console.log(extractDomain('http://www.domain3.com/123/abc'));
console.log(extractDomain('http://domain4.com/123/abc'));
console.log(extractDomain('https://www.domain5.com?123&abc'));
console.log(extractDomain('https://www.domain6.com/123/abc.html'));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering difficulty with retrieving information from a shared service on Angular 6

Attempting to pass JSON data from one component to another component (like parent to child) using a service. The service has two methods: setJsonData and getJsonData. Service Class import { Injectable } from '@angular/core'; @Injectable({ pr ...

Sending information to components in Angular using Router

Should I pass data through the angular router to a component, or is it better to use a service? Currently, the component is receiving data in the following way: this.account = activatedRoute.snapshot.data.account ...

Is it possible to invoke a method using a jQuery id selector within an Angular 2 template?

Is it possible to pass an id selector to a method from a template? <ul> <div id="temp">some text</div> <button (click)="methodName1(is it possible somehow? --> $('#temp'))">Label</button> </ul> ...

Issue with unresolved module in ESLint

Currently, I am utilizing the vss-web-extension-sdk in my project. To ensure the validity of my files, I have integrated ESLint along with eslint-plugin-import and eslint-import-resolver-typescript. import { WidgetSettings, WidgetStatus } from "TFS/Dashbo ...

Is there a way to access the Angular directive instance from the console?

ng.probe($0).componentInstance allows you to access the reference to the instance. Can we retrieve the directive instance from the console in any way? ...

The concept of self-referencing in TypeScript

I recently developed a Vue plugin that allows me to easily access constant objects in the templates of .vue files using shortcuts: <template> <span>{{ $consts.HELLO }}</span> </template> export default { constants: {HELLO: ...

How can I detect a click event in Angular using Typescript?

I am looking to transform the provided jquery scripts into typescript code. Jquery $(".room").click({ console.log("clicked"); }); TypeScript import { Component } from '@angular/core'; declare var $: any; export class AppComponent { } ...

Typescript navigation and Next.js technology

Currently, I am in the process of learning typescript and attempting to create a navigation bar. However, I encountered an error message stating "Unexpected token header. Expected jsx identifier". I am a bit puzzled by this issue. Could someone kindly pro ...

Tips for Embedding an External Website within an Angular Page without Using Web Elements

How can I create a global navigation that opens different apps on each nav link click, regardless of the technology they are built on? The challenge is that the navigation is in Angular and while we can integrate apps using Angular web elements if they a ...

The Order ID field in the Serenity-Platform's Order Details tab is not registering orders

I've been working on replicating the functionality of Orders-Order detail in my own project. https://i.stack.imgur.com/Bt47B.png My custom module is called Contract and Contract Line item, which I'm using to achieve this. https://i.stack.imgur ...

A tutorial on how to customize the hover effect for TableHead Column Identifiers in MaterialUI by adjusting

I'm struggling to customize the appearance of child th elements using the TableHead component from MaterialUI. While I've been successful in modifying various properties, I'm facing difficulty in changing the hover color. Below is the snipp ...

Tips for integrating a variety of components onto a single webpage

Exploring the functionality of Angular, I am looking to include multiple components on a single page. How can this be achieved effectively in Angular? I envision each div representing a distinct component and view, with all components residing in separate ...

Why is my Angular app displaying outdated data from Firebase when navigating between pages?

Currently, I am dealing with an Angular 9 application that is hosted on Firebase and utilizes Firestore for its data storage. There is a perplexing issue that I haven't been able to figure out completely despite simplifying the app extensively. I will ...

Building Angular 2 - Generate Multiple Sections in HTML with the Click of a Button

<div class="schedule-time"> <span>Include Schedule Time <span class="adr" (click)="AddNewSchedule()">+</span></span> ........ </div> <div *ngFor = "let i of OpenNewSchedule"> <div class= ...

Are undefined Static Properties an Issue in Mocked Classes? (Jest)

Currently, I am facing a challenge in mocking a class that includes a static property. jest.mock("../../src/logger/index"); import { Logger } from "../../src/logger/index"; // .. const LoggerMock = Logger as jest.MockedClass<typeof ...

VPS mysteriously terminates TypeScript compilation process without any apparent error

I've encountered an issue while trying to compile my TypeScript /src folder into the /dist folder. The process works smoothly on my local machine, but when I clone the repo onto my VPS (Ubuntu Server 22.04), install npm, and run the compile script, it ...

Seeking a filterable data grid in Angular 2

Has anyone come across a Sortable Table for angular4 that is readily available? I have seen examples of ng2table and Bootstrap table being integrated into Angular4 components with custom sort implementation. I am leaning towards using Bootstrap table with ...

The TypeScript function was anticipating one argument, however it received two instead

Can you help me fix the issue with my createUser() function? Why am I unable to pass parameters in Smoke.ts? Login.ts : interface User { url: string, email: string, } class Test{ async createUser(user: User) { await Page.setUrl(user.url); aw ...

Leverage the instance mesh feature in react-three-fiber to easily display a gltf model across multiple instances

Is there a way to efficiently render multiple instances of a 3D gltf model without causing performance lag due to multiple draw calls? I want to render around 100K instances and also be able to assign a color property to each model. How can instance mesh b ...

Interference of NestJS provider classes in separate event loops causing conflicts

I'm currently facing an issue where my shared library injectables are conflicting with each other. The bootstrap file initiates this file alongside a proxy server to start local microservices import { serviceA } from '@company/serviceA' imp ...