Splitting a URL twice to retrieve data from the string

My goal is to extract specific information from a URL:

Consider the following URL: https://www.example.com/Place/Name.html?randomtext

I want to retrieve only "Name" from outgoing links.

I attempted to achieve this using the following approach:

    function() {
      if ({{outgoing link}})
      var Name= {{Click URL}};
      return Name.split(""/")[2];
      return Name.split(""?")[0];
    }

So far, I have successfully extracted "place" with the following code snippet:

    function() {
          if ({{outgoing link}})
          var Name= {{Click URL}};
          return Name.split(""/")[2];

Answer №1

If you want to extract the file name ("Name"), you can use the following code snippet:

let fileName = url.substring(url.lastIndexOf('/') + 1).split(".")[0];

https://example.com/code-snippet

Answer №2

unsure if this meets your requirements:

const website = 'https://www.example.com/Location/Place.html?randomtext'

 function extractSegmentFromURL() {
      return website.split("/")[4].split(".")[0];
    }
    

const website = 'https://www.example.com/Location/Place.html?randomtext'

 function extractSegmentFromURL() {
      return website.split("/")[4].split(".")[0];
    }
    
    console.log(extractSegmentFromURL(website))

    console.log(extractSegmentFromURL(website))

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

Tips for building a live React app!

I'm looking to develop a real-time news application that displays a list of countries with the latest news next to each country name, automatically updating as new information is added to the API. For instance, I have an endpoint like this: (for Aust ...

Issue encountered: Unable to locate module during local npm package installation within React Application

When I created a new React app, I installed an npm package using the command npm i ../. However, when I tried to import the component in App.js, I encountered an error stating "Module not found: Can't resolve 'test-app-npm'". The package is ...

Potential memory leak detected in EventEmitter by Discord.js

One night while my bot was running on auto-pilot as I drifted off to sleep, a warning popped up out of the blue: MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 guildMembersChunk listeners added to [Client]. Use emitter.setMaxLi ...

Stop the browser from automatically loading a file that is dragged and dropped into it

I've implemented an HTML5 drag and drop uploader on my webpage. It functions perfectly when a file is dropped into the designated upload area. Unfortunately, if I mistakenly drop the file outside of that area, the browser treats it as a new page loa ...

Tips for setting up a scheduled event on your Discord server using Node.js

Hello fellow programmers! Recently, I've been working on a Discord bot using discordjs sdk. I'm trying to implement a feature where the bot creates an event every week. I went through the discordjs guide and checked the discord api documentati ...

Conceal different text when a div with a specific class includes particular text

Is there a way to dynamically add a class to a div based on the content of another div on the same page? Here is a snippet of my HTML: window.addEventListener("load", () => { let b = document.getElementsByClassName('aw-property-data-item') ...

Arrange ten objects in each of the four JavaScript arrays in ascending order based on a specific value

model with sample data -> [{ date: '13413413', name: 'asdfasdf', other: 'kjh' }] The getJSON function returns 4 arrays, each containing 10 model objects. array1 = 10 resultsObj sorted by date from newest to o ...

Documentation for Lambda function within an object

Looking to properly document the sock and data variables using JSDoc in my code. var exec = { /** * @param {Number} sock * @param {String} data */ 1: (sock, data) => { console.log("GG"); }, 2: (sock, data ...

MongoDB does not always insert the complete array

For inserting 499 entries into a subdocument from an array, I have the following code snippet: var bodyParser = require('body-parser'); var express = require("express"); var path = require("path"); var session = require('express-session&apo ...

Is it possible to include a div above a centered image?

Currently I am working with ImageFlow and I would like to overlay a div on top of the image when it is clicked, sliding it into the center. I have been looking through the JavaScript file but the function MoveIT() seems to be called multiple times and I am ...

Obtaining specific data from the forEach iteration, post-click event with JavaScript

Trying to access the value from a Tree-structured Array of Objects stored in Local Storage, https://i.sstatic.net/YJ1PI.png Created a function that retrieves values from local storage using forEach and displays them on the screen. https://i.sstatic.net/ ...

What is the best way to utilize a component function within Vue to delete an item from an array stored in the parent's data?

It might be more helpful for you to take a look at the VueJS code related to this and then I can provide some explanation: new Vue({ el: '#app', data: { history: [ {name: 'red', value: '#f00'}, ...

Trouble with apostrophes rendering in JavaScript on WordPress posts

My current challenge involves adding a post to Wordpress using an external script. This post includes a JavaScript section that contains an iframe for displaying movies. Knowing that Wordpress splits default tags, I have implemented a special plugin to han ...

What is the method for adjusting the amount of rows shown in ng-grid?

As I leverage ng-grid for data presentation, I notice that approximately 10 rows are visible before the vertical scrollbar appears and conceals the remaining data. Is there a way to adjust the threshold for when the vertical scrollbar kicks in? ...

Generating a collection of items within a JavaScript object

Struggling with sending a json object to a java API backend that requires an Object containing a list of objects. Wondering if it's possible to create a list of objects inside a javascript Object. While I know we can create an "Array" of objects with ...

Attempting to alter an image with a click and then revert it back to its original state

I'm currently working on a feature that toggles an image when a specific class is clicked. The code I have so far successfully switches from 'plus.png' to 'minus.png' upon clicking, but I need it to switch back to 'plus.png&ap ...

Issues with Vercel Next.js environment variable functionality not functioning as expected

I integrated Google Maps into my Next.js project successfully while working locally. The secret key for Google Maps is stored in next.config.js and accessed in the code through process.env.NEXT_PUBLIC_GOOGLEMAPS After deploying the project to Vercel, I de ...

Encountering an issue with React NextJS when attempting to upload a file and submit form data simultaneously through the NextJS API. The error message received is "501 Not

I am currently working on a Next.js application that includes a form where users can upload a file while providing their personal information. I have encountered an issue with Multer and the next-connect middleware, specifically when setting bodyParser to ...

`Async/await: Implementing a timeout after a fadeout once the fadein operation is

Here is a snippet of code that demonstrates the process: async function loadForm (data) { let promiseForm = pForm(data); await promiseForm.then(function(data) { setTimeout(function () { $container.fadeOut(function() { ...

Having a problem with the xmlhttprequest, not confident if it is being called correctly

I encountered a problem with the code I have where a user selects a sales center and it should trigger a currency change. Both selections are dropdowns, but when I choose a sales center, I receive an error saying ReferenceError: makeRequest is not define ...