0

My function to get query string is:

GetID: function(name) {
    return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1]);
     },

And when I got a url:

http://localhost/testsite/testB.aspx?ID=12

and call this function Its return 12.

But now my url pattern is change.

Current url pattern is:

  http://localhost/testsite/testB.aspx/12

Now I try to change regex and replace ? with / and other things to get desire value 12.

But I am not success.My knowledge about regex is not so good so help to modify my regex to get desire value.Thanks.

Eli
  • 14,779
  • 5
  • 59
  • 77
4b0
  • 21,981
  • 30
  • 95
  • 142
  • 1
    possible duplicate of [How can I get query string values?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values) – Ram Apr 04 '13 at 08:28
  • Change the title of your question to something related to regular expressions then. – emerson.marini Apr 04 '13 at 08:28
  • I want a help to change regex pattern bcz my url pattern is changed so how it duplicate? Given link only work when its has a common url not in special pattern. – 4b0 Apr 04 '13 at 08:33

2 Answers2

0

Your previous regex simply didn't care for the ? you are trying to replace by / (as I assume your name var contains ID).
Cannot do much with so few information about your URLs (you switched to get parameters to URL, they don't work the same way at all, what's the general form of your URLs?), but I'll give it a try:

/\d+$/

will simply get you the numbers at the end of your URL.

Loamhoof
  • 8,293
  • 27
  • 30
0

To get number 12 in both url string:

var myStr = "http://localhost/testsite/testB.aspx/12";

or

var myStr = "http://localhost/testsite/testB.aspx?ID=12";

You just need to use only 1 regex:

var number = parseInt(myStr.match(/[0-9]+(?!.*[0-9])/), 10);

or simplier:

var number = parseInt(myStr.match(/\d+$/), 10);

Demo: http://jsfiddle.net/RPjaq/

Demo: http://jsfiddle.net/RPjaq/1/

Eli
  • 14,779
  • 5
  • 59
  • 77