Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7642711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:15:34+00:00 2026-05-31T09:15:34+00:00

I need to build a string from the data contained in this url using

  • 0

I need to build a string from the data contained in this url using javascript/jQuery:

http://www.example.com/members/admin/projects/?projectid=41

The string returned should look as follows:

/ajax/projects.php?projectid=41

Obviously if there is no query string present, the method should still return a string of the same format minus the query string. e.g.

http://www.example.com/members/admin/messages/

should return…

/ajax/messages.php

I’ve made numerous attempts, all met without success due to my poor grasp of regular expressions, and it feels as though the ore I rad on the subject the more I am confusing myself.

If someone could help it would be greatly appreciated.

EDIT: The ‘admin’ portion of the url is a users ‘username’ and could be anything.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:15:35+00:00Added an answer on May 31, 2026 at 9:15 am

    Here’s a function that will take your URL and return a new one according to the rules you’ve listed above:

    function processURL(url) {
        var base = "", query = "";
        var matches = url.match(/([^\/\?]+)(\/$|$|\?|\/\?)/);
        if (matches) {
            base = matches[1];
            matches = url.match(/\?[^\?]+$/);
            if (matches) {
                query = matches[0];
            }
        }
        return("/ajax/" + base + ".php" + query);
    }
    

    And, a test app that shows it working on a bunch of URLs: http://jsfiddle.net/jfriend00/UbDfn/

    Input URLs:
    
    var urls = [
        "http://www.example.com/members/admin/projects/?projectid=41",
        "http://www.example.com/members/bob/messages/",
        "http://www.example.com/members/jill/projects/",
        "http://www.example.com/members/alice/projects?testid=99",
        "http://www.example.com/members/admin/projects/?testid=99"
    ];
    
    Output results:
    
    /ajax/projects.php?projectid=41
    /ajax/messages.php
    /ajax/projects.php
    /ajax/projects.php?testid=99
    /ajax/projects.php?testid=99
    

    To explain, the first regular expression looks for:

    a slash
    followed by one or more characters that is not a slash and not a question mark
    followed by one of the four sequences
        /$    a slash at the end of the string
        $     end of the string
        ?     a question mark
        /?    a slash followed by a question mark
    

    The point of this regex is to get the last segment of the path that comes before either the end of the string or the query parameters and it’s tolerant of whether the last trailing slash is there or not and whether there are any query parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to formulate a url string from data on a checkout page. I
I need to build a function which parses the domain from a URL. So,
I will need to be able to receive this xml data from a Java
(java 1.5) I have a need to build up a String, in pieces. I'm
I need to build a simple HTTP server in C. Any guidance? Links? Samples?
i have an string data array which contains data like this 5~kiran 2~ram 1~arun
I want to build a C# object from hierarchical XML data usinq LINQ. I
i have to build an small app in order to show some data from
I'm trying to use JQuery's $.getJSON to access Model data from the Controller, and
I need to write a system to generate HTML email from a data model

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.