Untangling REGEX URLs for Effective Filtering

I am currently working on creating a filter for a video list. I have successfully put together a string that includes all the values selected by the user for filtering. Right now, I am passing this string as a parameter through the URL.

For example:

NOTE: Keep in mind that the user may choose fewer filters, so the parameter:

status:3&site:4&categories:15,2,3&actors:33,5,36&search:Grass%20hopper%20
, can vary. The provided example represents the maximum combination.

QUERY: How can I extract all the data using Regular Expressions (JS)?

Desired output:

['status=3', 'site=4', 'categories=15,2,3', 'actors=33,5,3', 'search=Grass hopper']
.

Currently, I have come up with this expression:

(\w+):(((\d+),)*(\d+)|(\d+))&*
, but I am unsure of how to proceed.

Thank you for any assistance provided.

Answer №1

Your regular expression only matches digits with optional commas, not values like Grass hopper.

To match key-values in the last /.../ where the rightmost / is at the end of the string, you can use the following regex pattern:

\w+:(?!\/\/)[^&]+(?=[^\/]*\/$)

For a detailed explanation and to test the regex pattern, you can visit this demo.

Explanation:

  • \w+: - Matches 1 or more alphanumeric characters followed by a colon
  • (?!\/\/)[^&]+ - Matches 1 or more characters that are not & but does not start with //
  • (?=[^\/]*\/$) - Positive lookahead to ensure it's inside the last /.../

Another approach involves using a regex to extract URL parts after video/ (demo)

videos\/([^\/]+)

You can then split the captured value from match[1] with &:

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

How to resolve CORS issues in an AngularJS, Spring Boot, and Nginx setup

For the past few days, I've been struggling with an issue. I have a backend server set up using Spring-boot with Rest API. This server is being called from a frontend interface built with AngularJS and managed by Nginx. Everything is running locally ...

Removing jQuery error label from an HTML block

I need a single command that will remove an error label from my HTML when the field content is changed. It currently works on input and select elements, but not within an input-group. I am looking for a solution that will work universally across all instan ...

Having issues with the functionality of the Previous/Next button in my table

I am facing a challenge with my table as I am trying to include previous/next button for users to navigate through it. However, the interaction doesn't seem to be functioning properly, and I suspect that I need to establish a connection between the bu ...

The ConsoleCapture does not capture every console error for Sentry

Running into an issue capturing console errors with Sentry in a Next.js app. The problem arises from an error within a library that is inaccessible to us, specifically related to WebSocket "WebSocket is already in CLOSING or CLOSED state" This error is c ...

Securing a route using a referrer in Node.js: Best practices

Within my node.js application, I am looking to secure a specific route so that users can only access the page /post if they are coming from /blog. If the user accesses the route from any other source, they should be redirected to /. I have implemented the ...

Instructions for smoothly moving back to the top (or specific anchor) from the current view using AngularJS

As I prepare to delve into the world of Angular, a burning question comes to mind. Is it feasible to create an animation that scrolls up to the top of the page or a specific anchor point when transitioning to a new view after clicking a link while scroll ...

Cannot find a function within the Promise

Here is the code snippet I am working with: var c = function(address, abiJson){ var _ = this; this.data = { wallet: false, account:{ address: false }, contract:{ addre ...

Assistance needed for jQuery functions running too early

Within my click function, I am setting certain flags and then calling two functions. However, I want these two functions to execute sequentially in order after the flags have been set. jQuery("#element").click(function(e){ var op = jQuery("#box" ...

Searching for dates in an ng-repeat using AngularJS

I've implemented a global filter for my array of data, and it seems to be working fine overall except for the dates. I've done some research but couldn't find a clear solution. I've come across custom filters, but I'm wondering if ...

Issue with restricting table size in AngularJS not resolving

I need assistance with limiting the size of a table to 5. Here is my current code snippet: <tr ng-repeat="(key,value) in cla|limitTo:5"> <td class="mdl-data-table__cell--non-numeric">{{ key}}</td> <td class="mdl-data-table_ ...

retrieving the current value of a variable from a jQuery function

I've done my best to keep things simple. Here's the HTML code I've put together: <div id="outsideCounter"><p></p></div> <div id="clickToAdd"><p>Click me</p></div> <div id="in ...

Steps to make pop-up iframe function on the same page within a react nextjs application

My vanilla Html app has a pop-up feature that functions perfectly. When the button is clicked, the pop-up opens and everything works as expected. However, I am encountering an issue when trying to implement this same functionality in my React, NextJS app. ...

Best practices for handling multiple tables in AngularJS HTML

How can I loop through multiple tables in AngularJS? Here is an example of my HTML code: <div ng-repeat="stuff in moreStuff"> <table> <h1>{{stuff.name}}</h1> <tr ng-repeat="car in cars"> <td> ...

The console.log for a GET request in Express Router is displaying undefined parameters

After searching extensively on SO for a question similar to mine, I have come to the realization that my issue should be quite straightforward. The closest thread I discovered was this link. My current focus is on learning Node.JS, and I am in the process ...

Is there a way to remove a specific object key from a JavaScript object?

One thing I would like to achieve is transforming a JSON structure. Let's start with an example like this: { "de": { "errors.de.i18n.js": { "errors": { "addcreditcard": "Wir konnten diese Karte nicht verifizier ...

The variable within my function is not being cleared properly despite using a jQuery function

Recently, I encountered an issue with a function that displays a dialog box to ask users if their checks printed correctly. Upon clicking on another check to print, the "checked_id" value remains the same as the previously executed id. Surprisingly, this i ...

The Angular bootstrap popover vanishes as soon as the mouse hovers over it

Currently, I am facing an issue with an angular bootstrap popover on some text. The problem arises when the user tries to click on a link inside the popover as it disappears. Additionally, when changing from one popover to another, the previous one does no ...

Updating the CSS: Using jQuery to modify the display property to none

I am facing an issue with displaying an element that is defined as display:none in CSS. I tried to use the .show() function in jQuery, but it's not working as expected. Here's the code snippet: CSS .element { position: absolute; display: no ...

Receiving an error when attempting to inject the Router in a component constructor without using the elvis operator

Upon launching my app, I desire the route /home to be automatically displayed. Unfortunately, the Angular 2 version I am utilizing does not support the "useAsDefault: true" property in route definitions. To address this issue, I considered implementing th ...

Updating information on separate controllers in AngularJS

Starting out on this new framework. I have a query regarding updating a controller from another controller. I know this question has been asked numerous times. But my issue is unique to me. I can solve it using rootScope, but I'm not a fan (Am I ...