views:

52

answers:

3

Similar to my previous question:

http://stackoverflow.com/questions/3300433/spliting-a-string-in-javascript

The URLs have now changed and the unique number ID is no longer at the end of the URL like so:

/MarketUpdate/Pricing/9352730/Report

How would i extract the number from this now i cannot use the previous solution?

+2  A: 

If the URLs always look like that, why not use split() ?

var ID = url.split('/')[3];
Felix Kling
I'm not sure they will always look like that. They will always have a unique number though.
RyanP13
+4  A: 

You could search for

/(\d+)/

and use backreference no. 1 which will contain the number. Note that this requires the number to always be delimited by slashes on both sides. If you also want to match numbers at the end of the string, use

/(\d+)(?:/|$)

In JavaScript:

var myregexp = /\/(\d+)\//;
// var my_other_regexp = /\/(\d+)(?:\/|$)/;
var match = myregexp.exec(subject);
if (match != null) {
    result = match[1];
} else {
    result = "";
}
Tim Pietzcker
+1  A: 
urlstring = "/MarketUpdate/Pricing/9352730/Report"
$str = urlstring.split("/");
alert($str[3]);

This splits the string each time it finds the / symbol and stores it into an array, You can then get each word in the array by using $str[0]

dpmguise