开发者

Extracting number ID from a URL in Javascript

开发者 https://www.devze.com 2023-01-08 07:11 出处:网络
Similar to my previous question: spliting a string in Javascript The URLs h开发者_如何学编程ave now changed and the unique number ID is no longer at the end of the URL like so:

Similar to my previous question:

spliting a string in Javascript

The URLs h开发者_如何学编程ave 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?


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 = "";
}


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

var ID = url.split('/')[3];


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]

0

精彩评论

暂无评论...
验证码 换一张
取 消