Using Regex and Javascript to extract the base URL from a string

I am attempting to extract the base URL from a string without using window.location.

  • It must eliminate the trailing slash
  • It must be done using regex instead of New URL
  • It should handle query parameters and anchor links

In essence, all of the following examples should result in https://apple.com or https://www.apple.com for the last one.

  • https://apple.com?query=true&slash=false
  • https://apple.com#anchor=true&slash=false
  • http://www.apple.com/#anchor=true&slash=true&whatever=foo

These are just sample URLs, variations like

https://shop.apple.co.uk/?query=foo
should yield https://shop.apple.co.uk - It could be any URL like: https://foo.bar

The closest I've gotten is with:

const baseUrl = url.replace(/^((\w+:)?\/\/[^\/]+\/?).*$/,'$1').replace(/\/$/, ""); // Base Path & Trailing slash

However, this solution fails when dealing with anchor links and queries that directly follow the URL without a preceding /.

Do you have any suggestions on how to make it work for all scenarios?

Answer №1

If you include # and ? in your negated character class, there is no need for using .* as it will match until the end of the string.

To handle your example data, you can use the following pattern to match:

^https?:\/\/[^#?\/]+

Check out the regex demo here

strings = [
"https://apple.com?query=true&slash=false",
    "https://apple.com#anchor=true&slash=false",
    "http://www.apple.com/#anchor=true&slash=true&whatever=foo",
    "https://foo.bar/?q=true"
];

strings.forEach(s => {
    console.log(s.match(/^https?:\/\/[^#?\/]+/)[0]);
})

Answer №2

If you want an easier way to extract information from URLs, consider using Web API's built-in URL. It provides convenient access to parsed properties like query string parameters and protocols.

Using regex for this task can be needlessly complicated compared to the straightforward approach offered by the browser.

While I understand your question about regex usage, for those who prioritize simplicity in retrieving URL data over sticking with regex, this alternative method could prove helpful.

let one = "https://apple.com?query=true&slash=false"
let two = "https://apple.com#anchor=true&slash=false"
let three = "http://www.apple.com/#anchor=true&slash=true&whatever=foo"

let urlOne = new URL(one)
console.log(urlOne.origin)

let urlTwo = new URL(two)
console.log(urlTwo.origin)

let urlThree = new URL(three)
console.log(urlThree.origin)

Answer №3

    let mainUrl = url.replace(/(.*:\/\/.*)[\?\/#].*/, '$1');

Answer №4

If you want to extract everything before the .com section of a URL, you can use the following code snippet. Just remember to add .com back to the extracted part afterwards.

^http.*?(?=\.com)

Alternatively, you could try:

myUrl.Replace(/(#|\?|\/#).*$/, "")

This will remove anything after the host name in the URL.

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

Issue encountered while attempting to save hook arrays: Uncaught TypeError - 'choices' is not able to be

I'm in the process of creating a straightforward multiple-choice exam form that includes choices and answers. Whenever a user selects an option, it should be added to the array of choices. At the start, I have an array called exercises, which consist ...

Strategies for Handling Errors within Observable Subscriptions in Angular

While working with code from themes written in the latest Angular versions and doing research online, I've noticed that many developers neglect error handling when it comes to subscription. My question is: When is it necessary to handle errors in an ...

AngularJS testing typically involves the use of the inject module injection process

Currently, I am working on testing a service called documentViewer, which relies on another service named authService. angular .module('someModule') .service('documentViewer', DocumentViewer); /* @ngInject */ function Do ...

Stop the bubbling effect of :hover

How can the hover effect be prevented for the parent element when hovering over its children? Please take a look at the following code snippet: const parent = document.getElementById('parent') parent.onmouseover = function testAlert(e) { /* ...

"Expand your list in AngularJS by automatically adding more items if the ng-repeat count is

I am struggling with this section of code. It currently shows a box with the first four linked resource images. However, I need it to display additional images if there are less than four. I tried looking for a solution, but couldn't find one. <di ...

Instructions for correctly setting up the test-ai-classifier plugin with appium for selecting UI elements

Encountered an issue on Ubuntu 20.04. Referenced the steps provided at https://github.com/testdotai/appium-classifier-plugin Installed test-ai-classifier both under the appium path and globally using npm install -g test-ai-classifier Ensured no errors f ...

In what way can a property in JavaScript alter an object?

I am a newcomer to node.js, although I have been writing Javascript for many years. Recently, I encountered an interesting pattern that has left me puzzled: a Flag that is used to set a modifier on the object. For example, in the socket.io documentation: ...

Using the Set function to compare distinct elements within different arrays

Update: After reviewing the link shared by faintsignal, it appears to be the most suitable answer. It not only clarifies why this behavior is happening but also provides a solution to the issue at hand. I am currently working with an array and trying to d ...

Update the image using JavaScript whenever the button is pressed

Looking for a way to change the image every time the "next" button is clicked. Currently, the code only shows the last image from the 'book' array after a single click of the 'next' button. Is there a solution to this issue? HTML code: ...

combine a pair of elements simultaneously

I recently developed a nested directive for a multitabbed form, and here is a simplified version: <outer> <inner heading="a" src="'a.html'" active="true"></inner> <inner heading="b" src="'b.html'"></inner ...

Issues in the d3.js chart

I'm facing an issue with some incorrect lines appearing in my d3.js chart. Strangely, the problem seems to disappear when I switch tabs on Chrome and return to the chart's tab. It's puzzling to figure out the root cause of this issue, so I ...

Efficiently Organizing Data Using Coldfusion Loops in Columns and Rows

My issue lies in pulling data from a database to display on my website. I have three keys/attributes (columns) - PostedDate, DataImage, and Source that need to be shown together in one div with the posted date at the top, image in the middle, and source at ...

Unable to transfer file using ajax (displaying print_r($_FILES); Array ( ) )

I have encountered an issue with sending a file using XHR compared to a common form confirmation. Here is the HTML code: <form action="ajax/upload.php" method="post" name="form1" enctype="multipart/form-data" id="id1"> <input type="file" name=" ...

What are the steps for aligning GeoJSON data with terrain within Cesium Sandcastle?

I am currently using terrain view in Cesium Sandcastle and have loaded roads data in GeoJSON format as lines. I would like to clamp them on the terrain, similar to this example (select "Sample line positions and draw with depth test disabled" from drop-dow ...

Choosing the selected option in AngularJS

I am facing a frustrating issue with understanding how AngularJS manages select option values and selections. I have a basic item that I pass to a modal window, which includes a template_id. Additionally, I have a list of templates with names and ids, and ...

Navigation menu with submenus containing buttons

I attempted to incorporate a dropdown into my existing navigation bar, but unfortunately, the dropdown content disappeared after adding the necessary code. I am now at a loss on how to troubleshoot this issue and make the dropdown function properly. Despit ...

The Node application seems to be encountering an issue when attempting to handle

Whenever I click a button on my page in Node using Express, my JavaScript file sends the following request: toggleCartItem = index => { http = new XMLHttpRequest(); http.open("POST", `/cart_item/${index}`, true); http.send(); } Th ...

Is there a text form in Angular that allows only numerical input?

Here's an input form in Angular that I'm working on: <input ng-model="sc.zip" class="form-control" maxlength="5" type="text" /> I want to keep the form as a simple empty textbox without limiting it to only numbers. However, I do want to r ...

Tips on expanding the row number column width in jqGrid

Within my JQuery Grid, I am utilizing a parameter called rownumbers. jQuery("#dateInfo").jqGrid({ url : theURL, datatype : "json", sortable: false, colNames:['Date & Time'], colModel:[{name:'arrivalTime',index:'arrivalTime& ...

How can an array be generated functionally using properties from an array of objects?

Here's the current implementation that is functioning as expected: let newList: any[] = []; for (let stuff of this.Stuff) { newList = newList.concat(stuff.food); } The "Stuff" array consists of objects where each ...