Ways to retrieve several URLs from a given text

Here is a string containing three 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"

I am looking for a way to separate these URLs and store them in an array. The URLs can start with http, https, www, or any valid URL format. My desired output would be:

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

Answer №1

Give this a shot:

let url = "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"
url = url.split("http").map(item => { return "http"+item }).slice(1)

Start by splitting the string on "http", resulting in an array. Then add back "http" to each element of the string. However, since the first element in the array is just "http", you'll need to use slice to remove it.

Answer №2

To achieve the same result in Python, utilize this code snippet:

links_string = "https://spotify.com/song/lovehttps://www.youtube.com/watch?v=ABCD1234&list=playlist1234567https://www.youtube.com/watch?v=ABCD1234&list=playlist1234567"

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

If you wish to implement a similar functionality in JavaScript, consider exploring options like these.

Answer №3

const links = "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 urlsArray = links.split("https://").map(item => { return "https://" + item }).slice(1);
console.log(urlsArray);

Give this code a try to effectively split the https values within the string

Answer №4

let phrase = "https://amazon.com/product/1234https://www.netflix.com/shows/movies"
const httpText = phrase.split("http://").map(item => { return "http://"+item }).slice(1)
const httpsText = phrase.split("https://").map(item => { return "https://"+item }).slice(1)
const combinedArray = [...httpText, ...httpsText]
console.log(combinedArray)

Answer №5

If you want to include a space or any other delimiter before each occurrence of "http" and then split the string, here's another method:

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

Answer №6

There was a need for me to convert a string containing multiple URLs into an array. To achieve this, I utilized a regular expression to manage the URLs and employed the .match() method to generate the desired 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);
}

The output would be an array:

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

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

Mobile device experiencing issues with owl-carousel functionality

responsive property doesn't appear to be working as expected. While it functions correctly on desktop, the number of items displayed remains the same on mobile devices. $('.owl-carousel').owlCarousel({ loop:true, margin:5, nav:f ...

Fixing a menu hover appearance

I recently encountered a small issue with the menu on my website. When hovering over a menu item, a sub-menu should appear. However, there seems to be a slight misalignment where the submenu appears a few pixels below the actual menu item. Check out the w ...

Converting a string to a date type within a dynamically generated mat-table

I am working on a mat-table that shows columns for Date, Before Time Period, and After Time Period. Here is the HTML code for it: <ng-container matColumnDef="{{ column }}" *ngFor="let column of columnsToDisplay" > ...

Display the iframe website without it being visible to the user

Is there a way to load a separate website, such as a Wikipedia article, in an iframe on a webpage without freezing up the whole page when it becomes visible after clicking a "show" button? And if not, how can we display a loading gif while the iframe is ...

Guide on implementing asyncWithLDProvider from Launch Darkly in your Next.js application

Launch Darkly provides an example (https://github.com/launchdarkly/react-client-sdk/blob/main/examples/async-provider/src/client/index.js) showcasing how to use asyncWithLDProvider in a React project (as shown below). However, I'm struggling to integr ...

Listening for dates in NodeJS and triggering callbacks

Is there a method or module available that allows me to monitor the date and trigger a specific action when a certain condition is met without relying on setTimeOut? What I am looking for: if(currentHour==="08:00:00"){ doJob() } EDIT : To clarify, wha ...

Ways to change columns into rows with CSS

Hey there! I'm looking for a way to convert columns into rows and vice versa. In my table, I have both column headers and row headers on the left side. The row headers are simply bold text placed next to each row to describe its content. I want to op ...

PHP is unable to show items containing special characters such as double quotes and apostrophes

I am facing an issue with ordering food items. Names that do not contain apostrophes work fine, but those like Pasqua Nero D'Avola Sicilia are causing problems. I have tried replacing ' with \', but the issue persists. Here is the relev ...

Angular's UI Modal: utilizing inline template and controller functionality

I am looking to create a simple confirmation box using UI-modal, which I have used successfully for more complex modals in the past that load their template and controller from external files. However, this time I want something even simpler - just a basi ...

Save this page for later by using JavaScript to create a

Does anyone have a solution as to why window.location.href does not save the current webpage URL as a bookmark? Thank you ...

Can you explain the usage of array literal notation in javascript and provide examples of when it is most effective to

When running JSLint, I encountered the following error message: Issue identified at line 11 character 33: Please use the array literal notation [] instead. var myArray = new Array(); What exactly is the array literal notation and what&apo ...

What is the process for inserting an image using nodemailer in Outlook?

I have been facing an issue while trying to send emails via Gmail using Nodemailer. The problem arises when the emails are opened on Outlook or Windows 10 Mail by the company, as I need to embed images like the logo and a background. While attaching the im ...

Having trouble with rendering components in React and Bootstrap?

Attempting to display basic Bootstrap components using React. This corresponds to the index.html file: <!doctype html> <html> <head> <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script> <script src="j ...

Can TypeScript support the implementation of versatile decorators that can be linked together based on their input and output types?

Within our integration processes, we have developed templated implementations in our codebase that align well with the "pipe and filter" pattern in my opinion. These "components" can be structured in the following formats: class Component1<In, Out, Xi ...

The element type 'ReactElement<any>' does not match a JSX element constructor function. 'undefined' cannot be assigned to type 'Element | null'

After attempting the suggested fix of deleting node_modules/ and yarn.lock, then reinstalling everything, I still cannot resolve the issue. I am currently developing a basic router that renders children based on a certain prop: import React, { Fragment } ...

Mongoose is encountering issues with error handling due to the 'useUnifiedTopology: true' pass option

Recently, I discovered that by using the 'useUnifiedTopology: true' option in Mongoose, it eliminates the emission of an error if there are issues with the connection. For instance: mongoose.connect(DB, { useNewUrlParser: true, useCreateInde ...

Node-powered Angular

Currently working on setting up client-side routing with AngularJS and Node. Ran into some issues along the way. EDIT After making changes to my code based on recommendations from @PareshGami, following https://github.com/scotch-io/starter-node-angular, I ...

Sending files via an Ajax request triggers an unexpected page refresh

I have limited experience with ajax, but I am encountering an issue when trying to send form data to an ASP controller. Scenario 1 When I set data in the ajax request to $(this).serialize() All form data is successfully posted to the controller withou ...

The padding on the button inside the v-card is not being applied as expected

I am facing an issue with the following code snippet: <v-card height="200"> <v-card-actions class="mb-0"> <v-btn flat color="orange">Share</v-btn> <v-btn flat color="orange">Explore</v-btn> & ...

Strategies for extracting the specific array from an array list in JavaScript

I'm in the process of developing a calculator to delve deeper into the world of jQuery and JavaScript. I've designed buttons for each number ranging from 0 to 9. The goal is for the textbox to display the value of the button clicked. My initial a ...