Javascript - parsing a string to create a mathematical equation

Suppose I have an image URL structured like this:

blabla.com/bla/twofive.hash.png

This type of URL can be converted into a JavaScript string.

I am interested in learning how to create an array that breaks down the URL based on special characters, such as "." and "/", resulting in something like this:

var arr =  ['blabla', 'com', 'bla', 'twofive', 'hash', 'png'];

By doing this, I can easily access any element of the array using a simple arr[number];

Answer №1

give it a shot

"checkthisout.net/check/foursix.hash.jpg".split(/[./]/) //returns ["checkthisout", "net", "check", "foursix", "hash", "jpg"]

Answer №2

Here's a helpful tip:

You can use the following code snippet to split a URL into its individual parts:
'blabla.com/bla/twofive.hash.png'.split(/\.|\//); 

Outcome:

["blabla", "com", "bla", "twofive", "hash", "png"]

Answer №3

Another option is to utilize the match() method

let result = "example.com/test/four.seven.script.js".match(/[^./]+/g);
console.log(result);

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

Customizing the Switch component individually for each item fetched from an API in React Native

I'm struggling with setting the switch button individually for each item in my API. Despite trying multiple solutions, none of them seem to work for me. const results = [ { Id: "IySO9wUrt8", Name: & ...

Setting up a secure Node.JS HTTPS server on Cloud9 IDE

Wondering if it's possible to set up a Node.js https server in the cloud9 IDE? Check out this example of a basic https server setup in Node.js. var https = require('https'); var fs = require('fs'); var app = require('./app& ...

Switching a jQuery AJAX Response

Currently, I am utilizing an AJAX function to retrieve and display specific categorical posts when a corresponding button is clicked: <script> // Brochure AJAX function term_ajax_get(termID) { jQuery("#loading-animation").show(); ...

Iterating through an array with a .forEach loop and referencing the variable name

Currently, I am working on a project using JS/React and dealing with nested arrays in an array. The structure of my data looks like this: const ezra = ["Patriots", "Bears", "Vikings", "Titans", "Jets", "Bengals"] const adam = ["Chiefs", "Cowboys", "Packer ...

Guide on parsing an Array of arrays using JSON.parse

I have received a JSON.stringified Array of arrays in a variable named request. alert(request); When I alert the above, I get the following message: "[[\"0\",\"MahaShivRatri\"],[\"0\",\ ...

Exploring the functionalities of can-deactivate without incorporating routing

I'm facing an issue with a parent component and multiple child components. The parent component contains a form, while each child component also has its own form. Unfortunately, all child components share the same route as the parent component and hav ...

Enhancing jQuery Mobile listview with voting buttons on each item row

I am looking to incorporate 2 vote buttons within a jQuery mobile listview, positioned on the left-hand side and centered within each list item. While I have managed to achieve this using javascript, my goal is to accomplish it without any additional scrip ...

DataGrid Filtering with Material-UI

I recently started working on a React project and I'm trying to incorporate the mui datagrid component. I want to include filters for '>' and '<', but I couldn't find any information in the documentation. Is there a spec ...

Utilizing internal PDF links in a Microsoft UWP application

In the process of developing a UWP app using javascript, I have created a list of links that are connected to PDF files stored locally in the app. The ultimate goal is to offer a collection of hands-free documentation for the Hololens (Windows AR) device. ...

Showing nested arrays in API data using Angular

I would like to display the data from this API { "results": [ { "name": "Luke Skywalker", "height": "172", "mass": "77", & ...

Endless cycle within the while loop without any obvious cause

I've been tinkering with a timer and thanks to some feedback I received in this community, everything is running smoothly. Here's what the current version looks like for reference: https://i.stack.imgur.com/Qd7ll.png Here's a snippet of my ...

Implementing a codeigniter delete confirmation box with Javascript

I have tried numerous solutions on this site, but nothing seems to be working for me. I am trying to implement a pop-up window confirmation before deleting my data. Here is the code I am using: <a href="<?php echo site_url('admin/barang/delet ...

Is there a way to retrieve the file path of the file that is imported using an alias in Vite (Vue3)?

Hello! I have a few inquiries. Is it feasible to obtain the file path of the file utilizing an alias for import in Vite (Vue3)? Setup Here's the directory structure I'm using, purely for illustrative purposes: src/ module_a/ some_ ...

Ensure that the query is properly separated from the body when making an Axios request

While working on my code, I encountered an issue when making a PUT request to the backend of the application using Axios. The problem arose when the method in the user service, responsible for handling the request, returned a null response. Here is the met ...

Combining Strings in CIn the programming language C,

I am currently working on creating an array of structs and I need to concatenate a string value with the for loop index. Here is how I define the struct: typedef struct b { char title[30]; char author[40]; int year, price; } book_t; Next, I ...

When the browser's inner width is less than the inner height, use jQuery or JavaScript to show a message on the webpage

I've been having trouble getting this to work and I've attempted various solutions suggested by different sources such as: Display live width and height values on changing window resize php echo statement if screen is a certain size And more, b ...

Error 404 encountered while trying to access a website with parameters using Vue.js

Currently, I am in the process of building a website using VueJS and recently discovered how to use url parameters. Everything was working perfectly on my local machine - I could easily navigate to different pages by including parameters in the URL. For e ...

The span's onclick function seems to be malfunctioning

I am encountering an issue where the Onclick event is not triggering on a specific tag. Strangely, the same onclick event works perfectly fine when bound to an image element. I am currently developing a follow and unfollow feature using PHP and jQuery. How ...

Converting text file into an array or list

I am looking to read the contents of a .txt file and save the data to an array or list. The file is structured like this: row11 row12 row13 row21 row22 row23 row31 row32 row33 There are only spaces between the strings. After extracting ...

Switching between three div elements along with two icons can be done by using toggle functionality

Is there a way to make the icon change back when clicked again without making major changes to the existing code? Looking for suggestions on how to achieve this functionality. function myFunction() { var element = document.getElementById("demo"); el ...