0

I think I need regex, but my regex noobtown.

given this string:

/MyUrl?query=http://www.domain.com/category/index.jsp%3FcategoryId&bonkers=Upp&this=that

how can I simply just get the domain out of this: So remove the preceeding

/MyURL?query=

and then everything from and including &bonkers and onward. Basically just need the straight url with no parameters so the result looks like

http://www.domain.com/category/index.jsp%3FcategoryId%3D4414047

Knowing absolutely nothing about regex I got this far

.*url\?q=(.*)&sa 

but that doesn't quite do it. I'm attempting to parse a string in jquery to just return each URL in a matched select

$(data).find(".link a").each(function(){
                    console.log($(this).attr('href'));
                    //returns string with garbage before and after
                  });
user1888959
  • 329
  • 2
  • 11
  • 1
    possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – showdev May 27 '15 at 22:59
  • Could you supply a couple sample input and outputs please? I don't seem to be totally following where the matched string should end. Maybe post a link where your regex fails and include the desired output? – ShellFish May 27 '15 at 23:05

1 Answers1

0

Hi something like this would work. You declare the parameters you want to get out of your query string i.e. Query and then do a regex to only bring back your full query string with that parameter.. then a alert will tell you what it is.

  function getQueryStringValue() {
        var Query = "query";
        Query = Query.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + Query + "=([^&#]*)"),
            results = regex.exec(location.search);
        var QueryString = results === null ? "" :
decodeURIComponent(results[1].replace(/\+/g, " "));
        alert(QueryString);
    }
Josh Stevens
  • 3,943
  • 1
  • 15
  • 22
  • Maybe explain the regex to the OP so he actually learns something? – ShellFish May 27 '15 at 23:01
  • This is pretty much the [first answer of the duplicate](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript#answer-901144). – showdev May 27 '15 at 23:02