0

I have searched stack overflow for a Javascript solution (in the jQuery library) to get the URL parameter.

I have this function which does it smoothly:

// get the firstname value from the myurl - http://mysite.com?firstname=dany&lastname=dughy

var myurl = $("#gup").data("url");
var name = "firstname";

function gup(name, myurl) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec(myurl);
    if( results == null )
        return "";
    else
        return results[1];
}

I understand how it works, you set a regex and than execute that regex and retrieve the results in an array.

Can someome please explain the regex to me? I can't seem to understand it.

Ty

2 Answers 2

2

Let's call the function with gup("NAME","http://domain.com?NAME=value&name2=value2").

function gup(name, myurl) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");

This line escapes square brakets so it works nicely in the regex query, making "[]" into "\\\[\\\]".

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

Now the name is parsed to work in the regex. It will look like "[\\?&]NAME=([^&#]*)".

"[\\?&]" means find either ? or &. Because ? is a special character, to match a ? in text you need to escape it like \?, so this regex would become [\?&] but in javascript strings you need to escape the \ again. So [\?&] becomes "[\\?&]" in a string.

=([^&#]*) matches a literal = followed by any character except an & or #.

    var regex = new RegExp( regexS );
    var results = regex.exec(myurl);
    if( results == null )
        return "";
    else
        return results[1];

Returns the captured group, which is at position 1. Position 0 is the whole match.

}

Function call returns value

Sign up to request clarification or add additional context in comments.

5 Comments

I really did not know you had to escape strings again in JS (which confused me) and I definitely need to work some more on practical regexes. You opened a great path for me. Ty!
Won't [\\?&] find \, ? or & though? Regexp tester gives me this: i.imgur.com/UM0Pcok.png
@eithed yes, it would if it were plain regex but it is in a JS string, so you need to escape the \ that is used for escaping the ?
@eithed, you are testing it incorrectly. The point is being in a JS string. This shows what happens to a JS string: new RegExp("[\\?&]"). Then execute this: "i\\love?programming&too".replace(/[\?&]/gi,""). Besides, the \ is not used as a separator in URLs so the point is mute.
@Gerard Sexton, ah, yes, now I know what you mean. Thank you for explanation and your time!
0

Let's say that you're searching for firstname, then the regexp becomes: [\\?&]firstname=([^&#]*). Now, what it means is: [\\?&] search for any of characters \, ? or & that has firstname= afterwards, and [^&#]* = anything but & or # characters afterwards

2 Comments

Ty very much for answering. I believe Gerard's answer is more comprehensive; this is why I have accepted it.
No worries :) Regexpes are funny like that. I think though that there's a small mistake in the explanation of [\\?&], as it means \, ? or & (well, at least I think it does :D). Let me put this under his answer as a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.