1

I have got some URLs like that

/WebSite/Movies/807?sort=MovieName&sortdir=DESC&p2=2&p3=3

/WebSite/Movies/807?sort=MovieName&sortdir=ASC&p4=4&p5=5

and I need to get only part like

/WebSite/Movies/807?sort=MovieName&sortdir=DESC

/WebSite/Movies/807?sort=MovieName&sortdir=ASC

How it can be done via JavaScript Regex or JQuery?

I know there some online tools like http://regexpal.com/ and etc but I cannot figue out how to determine that string ends with DESC or ASC.

Thank you!

ahren
  • 15,967
  • 5
  • 44
  • 67
DmitryBoyko
  • 32,983
  • 69
  • 281
  • 458
  • 1
    You don't need to use regex for this. Look up the `.split()` method in javascript. – ahren Aug 15 '12 at 21:10
  • @ahren The problem is that it might be DESC or ASC and split() method doesnt't allow multiple separators as I know... – DmitryBoyko Aug 15 '12 at 21:12
  • Wouldn't it be better to parse the query string and filter out any parameters you don't want? – Brad Aug 15 '12 at 21:18
  • @Brad Thanks! Could you please provide any sample how to do it? I am not sure what do you mean... – DmitryBoyko Aug 15 '12 at 21:21
  • 2
    @Peretz, Check out http://stackoverflow.com/questions/2090551/parse-query-string-in-javascript Since your parameters can happen in any order, unless you have a very specific use case, you should be simply filtering out the parameters and re-building the query string. – Brad Aug 15 '12 at 21:23

4 Answers4

4

YOu can do it with substring

var sub = "";
if (string.indexOf("DESC") !== -1) {
    sub = string.substring(0, string.indexOf("DESC") + 4);
}
else if (string.indexOf("ASC") !== -1) {
    sub = string.substring(0, string.indexOf("ASC") + 3);
}

alert(sub);
1

Here is a regex pattern that will match anything up through desc or asc:

    .+[(DESC)(ASC)]
1

Try this (with JavaScript and jQuery just for fun)):

/.*(DESC|ASC).*/g

Look at the groups (#2 has what you are looking for)

<input id="Link" value="/WebSite/Movies/807?sort=MovieName&sortdir=DESC&p2=2&p3=3" style="width: 90%;" />
<br />
<button onclick="getOrder();">Get Order</button>
<script>
getOrder = function() {
    var url = $('#Link').val()
        , regex= /.*(DESC|ASC).*/g
        , match = regex.exec(url);

    alert(match[1]);
}
</script>
Jason Sperske
  • 27,420
  • 8
  • 63
  • 116
1

I think this has been answered already, but here's a fn I use all the time that I'll share with you. It returns the value you place in "default_" if the query string you are looking for ("key") does not exist. Otherwise it returns the value of "key".

I don't remember where I got it from. I picked it up from somewhere and made some gentle refinements.

    getQuerystring = function(key, default_){
        if (default_==null) default_=""; 
        key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
        var qs = regex.exec(window.location.href);
        if(qs == null)
           return default_;
        else
           return qs[1];
    }