user8583769
user8583769

Reputation:

How can i trim a url in react native

How can I trim the below URL using javascript in react native? this is the URL

"https://economictimes.indiatimes.com/industry/banking/finance/banking/is-indias-current-banking-crisis-just-old-wine-in-a-new-bottle/articleshow/71451268.cms"

I need to trim this to economictimes.indiatimes.com.

Upvotes: 0

Views: 765

Answers (3)

Abito Prakash
Abito Prakash

Reputation: 4770

You can do something like this

const url = "https://economictimes.indiatimes.com/industry/banking/finance/banking/is-indias-current-banking-crisis-just-old-wine-in-a-new-bottle/articleshow/71451268.cms";

const result = new URL(url).host;
console.log(result); // prints "economictimes.indiatimes.com"

You can see more about the URL interface here

Upvotes: 2

Arsalan Akhtar
Arsalan Akhtar

Reputation: 389

url = "https://economictimes.indiatimes.com/industry/banking/finance/banking/is-indias-current-banking-crisis-just-old-wine-in-a-new-bottle/articleshow/71451268.cms"

console.log(url.split("//")[1].split('/')[0]);

Try this!

Upvotes: 0

roottraveller
roottraveller

Reputation: 8232

try this.

url = "https://economictimes.indiatimes.com/industry/banking/finance/banking/is-indias-current-banking-crisis-just-old-wine-in-a-new-bottle/articleshow/71451268.cms"
var arr = url.split("/");
console.log(arr[2]);

Upvotes: 0

Related Questions