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

  • SEARCH
  • Home
  • 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 4086238
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:41:53+00:00 2026-05-20T18:41:53+00:00

Can someone help me with a regular expression that would help me do this.

  • 0

Can someone help me with a regular expression that would help me do this.

I have a url: http://www.site.com/foo/bar

when a user clicks on a link it will append it to the url making it look like this

http://www.site.com/foo/bar/link1

now for the tricky part, when a second link is clicked it keeps appending to the url like this

http://www.site.com/foo/bar/link1/link2

i would like for link2 to replace link1 making it http://www.site.com/foo/bar/link2

I want to note that the only reason links are appending like this are because I am using this expression to remove the '#' from the link and replacing it with '/'

location.assign(location.href.replace(/\/?#/g, "/"))

without the # the browser will not know to replace the hash marks but its causing me issues with my current setup.

Otherwise the link will look like http://www.site.com/foo/bar#link1 witch doesn’t exist with my setup

I’m using htaccess to replace page.php?type=foo&user=bar&page=link1 http://www.site.com/foo/bar/link1

so ya, my question is how to remove /link1 with /link2 BUT append link2 IF /link1 isn’t present

I want to keep the code as dynamic as possible since the link names could change.

EDIT:
My code

var newHash    = "",
        shref      = "",
        content    = '#content',
        $c         = $("#content"),
        $cw        = $("#content-wrapper");

    $(".menu-link, #my-account-link").live('click', function(){
        shref = $(this).attr("href").split("/");
        //window.location.hash = shref[5];
        //location.assign(location.href.replace(/\/?#/g, "/"))
        window.location.hash = $(this).attr("href");
        //console.log(window.location.hash);
        return false;
    });

    $(window).bind('hashchange', function(){
        /*if (location.href.indexOf("#") > -1) {
            location.assign(location.href.replace(/\/?#/g, "/"));
        }*/
        //TODO:: Find # replace with / then find first (or maybe 3rd/) and replace everything until the next / with ""
        newHash = window.location.hash.substring(1);
        console.log(newHash);
        if(newHash)
        {
            $cw.find(content).fadeOut(200, function() {
                $cw.load(newHash + " #content-wrapper", function() {
                    $c.fadeIn();
                });
            });
        }
    });
    $(window).trigger('hashchange');
  • 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-20T18:41:53+00:00Added an answer on May 20, 2026 at 6:41 pm
    var newElement   = document.location.hash.replace('#', ''); // after hash
    var pathElements = document.location.pathname.split('/');
    var lastElement  = pathElements[pathElements.length - 1];
    var endElements  = ['link1', 'link2', 'link3'];
    
    // the final element not in list
    if(endElements.indexOf(lastElement) === -1) {
      pathElements.push(newElement);
    }
    // replace last element
    else {
      pathElements.pop();
      pathElements.push(newElement);
    }
    
    document.location.pathname = pathElements.join('/');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this regular expression: @(?i)\b(?:p(?:ost)?\.?\s*[o0](?:ffice)?\.?\s*b?(?:[o0]x)?|b[o0]x) Can someone help me identify the issue and
Can someone help by showing me how to sort a LINQ expression. I have
Can someone help me write the regular expression for validates_format_of. It should fail if
Can someone help me with a regular expression to include A-z a-z 0-9 -
I have a very basic regular expression that I just can't figure out why
Can someone help my pea brain figure out why my simple regular expression is
I have the following URL: http://somedomain.com/aa/search/search.php I want it to return 2 selections, that
Can someone help me understand how to write this case statement properly its not
can someone help to write a method that converts a byte array to a
Can someone help my regex-challenged self with this? The match must start with *.

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.