1

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"
]
11
  • 1
    Yikes! Unfortunately, that string is ambiguous. URLs can contain https:// within them, so it isn't going to be possible for you to separate them unless you know something about the URLs. For example, if you're always dealing with YouTube, this isn't going to be a problem. But if you have to handle arbitrary URLs, this isn't going to work reliably. Where are you getting the string from? Can you change it? Commented Sep 17, 2020 at 4:07
  • i am getting this string from textarea value. Form the value I want to extract the urls only. Commented Sep 17, 2020 at 4:10
  • like this - urlChecker(value : string){ let textValue = value.replace(/\s{2,}/g, ''); let urls : Array<any> = []; let urlRegex = new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?([^ ])+"); if (textValue != '' && urlRegex.test(textValue)){ let matchedIfUrl = urlRegex.exec(textValue); if(matchedIfUrl != null && matchedIfUrl != undefined){ textWithUrl = matchedIfUrl[0]; console.log(textWithUrl); } } } Commented Sep 17, 2020 at 4:12
  • Are you sure these aren't coming in as separate lines? Not sure why someone would paste this. Commented Sep 17, 2020 at 4:16
  • 1
    @RAHULKUNDU The right solution is to redesign the source of generating these URLs to get them separately. Commented Sep 17, 2020 at 4:46

6 Answers 6

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

with http can i check www in here, like if any starts with only www. because I am getting that from textarea value and i have removed new lines from the value and sting the string is var t
0

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.

1 Comment

This is a JavaScript question.
0

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

Comments

0
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)

Comments

0

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"]

Comments

0

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' ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.