RAHUL KUNDU
RAHUL KUNDU

Reputation: 955

How to extract multiple URL from string

I have string like this - "https://gaana.com/song/dil-chahte-hohttps://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APmhttps://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APm"

In here I have three urls, how can i separate them and push them into an array. the url's can be start with http or https or directly from www or any valid url. I want the output like -

[
"https://gaana.com/song/dil-chahte-ho",
"https://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APm",
"https://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APm"
]

Upvotes: 1

Views: 1961

Answers (6)

JoeHogans
JoeHogans

Reputation: 11

For some reason I had to convert a String with multiple URLS to an Array. So I used a regex to handle the urls and .match() method for creating an Array.

let urls = '[https://website.com/slug, https://website.com/other-slug]';

function convertUrlStringToArray(urls) {
    let expression = /\b(https?:\/\/\S*\b)/g;
    let regex = new RegExp(expression);

    return urls.match(regex);
}

Output an Array: [ 'https://website.com/slug', 'https://website.com/other-slug' ]

Upvotes: 0

V.Tur
V.Tur

Reputation: 1427

One more way add to space or other delimiter before http and split string:

const str = "https://gaana.com/song/dil-chahte-hohttps://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APmhttps://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APm";
const result = str.replace(/http/g, ' $&').split(' ');
// result ["", "https://gaana.com/song/dil-chahte-ho", "https://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APm", "https://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APm"]

Upvotes: 0

HungNguyen
HungNguyen

Reputation: 1

const string = "https://gaana.com/song/dil-chahte-hohttps://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APmhttps://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APm"
const http = string.split("http://").map(val => { return "http://"+val }).slice(1)
const https = string.split("https://").map(val => { return "https://"+val }).slice(1)
const array = [...http, ...https]
console.log(array)

Upvotes: 0

pgksunilkumar
pgksunilkumar

Reputation: 396

var urls = "https://gaana.com/song/dil-chahte-hohttps://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APmhttps://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APm"
urls = urls.split("https://").map(val => { return "https://"+val }).slice(1)
console.log(urls);

try this one it will help to split https value in the string

Upvotes: 0

user14246674
user14246674

Reputation:

You can do that with the following code in Python:

links_str = "https://gaana.com/song/dil-chahte-hohttps://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APmhttps://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APm"

links = ["http"+str(link) for link in links_str.split("http")][1:]

Maybe you can try to do something like this in JavaSript.

Upvotes: 0

Chris
Chris

Reputation: 2374

Try this:

var t = "https://gaana.com/song/dil-chahte-hohttps://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APmhttps://www.youtube.com/watch?v=MWnFCGXjjS0&list=PLs1-UdHIwbo5p-8wh740E7CRhIoKq5APm"
t = t.split("http").map(x => { return "http"+x }).slice(1)

First split the string on "http", which gives you an array. Then append the http to each element of the string. Unfortunately you end up with the first element in the array being "http" that's why you need to slice to remove it.

Upvotes: 1

Related Questions