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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:35:34+00:00 2026-05-24T07:35:34+00:00

I have found the following JS on the web. It is a function to

  • 0

I have found the following JS on the web.

It is a function to get url params values.

function get_url_param(param) {
  param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+param+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec(window.location.href);
  if( results == null )
    return '';
  else
    return results[1];
}

However always when I see a exec() function I think: Eeek!

So my question is: is it safe?

Side bet: If you think this function sucks and have a better option don’t hesitate to share 🙂

The above function uses the real url but I only need to parse a string which contains an URL.

  • 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-24T07:35:35+00:00Added an answer on May 24, 2026 at 7:35 am

    Regexp#exec is safe, albeit not a very nice interface.

    Side bet: If you think this function sucks and have a better option don’t hesitate to share 🙂

    yeeep 🙂

    param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    

    This doesn’t use a global regexp so you are only replacing one instance of each bracket; field[][] wouldn’t work. Also you don’t need the character group… param.replace(/\[/g, '\\[') would have worked. Or, the non-regexp replacement idiom, param.split('[').join('\\[').

    Then:

    var regexS = "[\\?&]"+param+"=([^&#]*)";
    

    you’re not escaping nearly enough characters to be able to drop them into a regexp and have them mean their literal selves. See this question for a more watertight alternative.

    Anyway this kind of regex hacking still isn’t a good way of parsing URLs/query strings. This doesn’t deal properly with ; or %-encoding, or + for space, and it may trip on parameter lookalikes elsewhere in the URL.

    Instead, let’s first get the query string on its own. If you have a link or location object, you can get it from the the .search property . If you only have a string URL, you can turn it into a link object to get this reliably:

    function getQueryString(url) {
        var a= document.createElement('a');
        a.href= url;
        return a.search;
    }
    

    Now you can parse it into by dropping the leading ?, splitting on & or ;, then dropping the URL-decoded results into a JS Object:

    function parseQuery(query) {
        var lookup= {};
        var params= query.slice(1).split(/[&;]/);
        for (var i= 0; i<params.length; i++) {
            var ix= params[i].indexOf('=');
            if (ix!==-1) {
                var name= decodeURIComponent(params[i].slice(0, ix));
                var value= decodeURIComponent(params[i].slice(ix+1));
                if (!(name in lookup))
                    lookup[name]= [];
                lookup[name].push(value);
            }
        }
        return lookup;
    }
    

    This makes it easy to look up parameters:

    var url= 'http://www.example.com/?a=b&c=d&c=%65;f[]=g#h=i';
    var pars= parseQuery(getQueryString(url));
    
    alert(pars.a);      // ['b']
    alert(pars.c);      // ['d', 'e']
    alert(pars['f[]']); // ['g']
    alert('h' in pars); // false
    

    If you don’t need to read multiple values for a parameter, you could just do lookup[name]= value instead of the if...[]...push dance, to return single string values in the lookup instead of lists.

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

Sidebar

Related Questions

On the MSDN, I have found following: public event EventHandler<MyEventArgs> SampleEvent; public void DemoEvent(string
I have found the following command in AWK useful in Vim :'<,'>!awk '{ print
I have found the following expression which is intended to modify the id of
I have found the following resources on Balanced Matching for .net Regexes: http://weblogs.asp.net/whaggard/archive/2005/02/20/377025.aspx http://blogs.msdn.com/bclteam/archive/2005/03/15/396452.aspx
Reading a book I have found the following statement: (Object) Behaviors answer either of
Searching online, I have found the following routine for calculating the sign of a
I'm converting an application to use Java 1.5 and have found the following method:
According to what I have found so far, I can use the following code:
Hello this is may first question and I have found so far the following
I have the following function: def storeTaggedCorpus(corpus, filename): corpusFile = codecs.open(filename, mode = 'w',

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.