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 7941341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:38:31+00:00 2026-06-03T23:38:31+00:00

My urls will look like: http://example.com/whatever#page?x=1&locale=hu&y=2 http://example.com/whatever#page?x=1&locale=hu http://example.com/whatever#page?locale=hu http://example.com/whatever#page?locale= http://example.com/whatever#page?x=1 http://example.com/whatever#page http://example.com/whatever I’d like

  • 0

My urls will look like:

http://example.com/whatever#page?x=1&locale=hu&y=2
http://example.com/whatever#page?x=1&locale=hu
http://example.com/whatever#page?locale=hu
http://example.com/whatever#page?locale=
http://example.com/whatever#page?x=1
http://example.com/whatever#page
http://example.com/whatever

I’d like to get the locale parameter or empty string if it’s not set.

I’m trying something like:

locale = location.hash.replace(/.*(?:[?&]locale=([^&]*))?.*/, "$2");

But my problem is that I couldn’t find the right RegExp that works for all cases (both when there’s locale= in the hash and when there isn’t)

  • 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-06-03T23:38:34+00:00Added an answer on June 3, 2026 at 11:38 pm

    Here’s a piece of code that will extract it from the hash and avoid it anywhere else in the URL:

    function getLocaleFromHash(url) {
        var match = url.match(/#.*[?&]locale=([^&]+)(&|$)/);
        return(match ? match[1] : "");
    }
    

    And, you can see it work on all your test cases here: http://jsfiddle.net/jfriend00/p37Mx/


    If you want to be able to look for any parm in the hash, you would use this:

    function getParmFromHash(url, parm) {
        var re = new RegExp("#.*[?&]" + parm + "=([^&]+)(&|$)");
        var match = url.match(re);
        return(match ? match[1] : "");
    }
    

    See it work here: http://jsfiddle.net/jfriend00/6kgUk/


    A more generic function that will fetch all parameters in the URL would look like this. For normal URLs where the hash is after the query and the parameters are in the query string, it would look like this. This is a bit more code because it does more. It fetches all the parameters into an object where you can look up any parameter by it’s key and it URL decodes them all too:

    function getParmsFromURL(url) {
        var parms = {}, pieces, parts, i;
        var hash = url.lastIndexOf("#");
        if (hash !== -1) {
            // remove hash value
            url = url.slice(0, hash);
        }
        var question = url.lastIndexOf("?");
        if (question !== -1) {
            url = url.slice(question + 1);
            pieces = url.split("&");
            for (i = 0; i < pieces.length; i++) {
                parts = pieces[i].split("=");
                if (parts.length < 2) {
                    parts.push("");
                }
                parms[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
            }
        }
        return parms;
    }
    

    For a special version that handles parameters in a hash value and after a ? in the hash value like in the OP’s question (which isn’t the typical case), one could use this:

    function getParmsFromURLHash(url) {
        var parms = {}, pieces, parts, i;
        var hash = url.lastIndexOf("#");
        if (hash !== -1) {
            // isolate just the hash value
            url = url.slice(hash + 1);
        }
        var question = url.indexOf("?");
        if (question !== -1) {
            url = url.slice(question + 1);
            pieces = url.split("&");
            for (i = 0; i < pieces.length; i++) {
                parts = pieces[i].split("=");
                if (parts.length < 2) {
                    parts.push("");
                }
                parms[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
            }
        }
        return parms;
    }
    

    Working demo: http://jsfiddle.net/jfriend00/v8cd5/

    And, then if you wanted the local option, you’d just do this:

    var parms = getParmsFromURL(url);
    var locale = parms["locale"];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a site whose URLs look like http://www.example.com/NY-2010/ http://www.example.com/NY-2010/location/ http://www.example.com/NY-2010/something-else/ http://www.example.com/Washington-2009/ http://www.example.com/Washington-2009/location/ http://www.example.com/Washington-2009/something-else/
I would like to have these urls as categories on my page: http://example.com/en/articles //for
Example URLs http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I Any regex that will pull the correct YID
I have a list of URLs such as, http://www.mywebsite.com/page.php?genus=A_GENUS&species=A_SPECIES&id=12345 . I would like to
I'm trying to map certain routes so that auto generated Urls will look like
Should my URL's in Rails look like: http://foobar.com/articles?category=recent - OR - http://foobar.com/articles/recent I find
I have a bunch of urls like these. $urls = array( 'https://site1.com', 'https://www.site2.com', 'http://www.site3.com',
I have a website where my present geeky urls look like: http://www.bestatdubaiholidays.co.uk/pages/Quote/ Details.aspx?GUID=01a25b0c-e0ac-40ba-abd1-298f3abd9612 I
I have the need to have the urls in my bosses application look like:
I have an application that will accept URLs from the built in web browser

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.