regex - Extracting number ID from a URL in Javascript - Stack Overflow

Similar to my previous question:spliting a string in JavascriptThe URLs have now changed and the unique

Similar to my previous question:

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?

Similar to my previous question:

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?

Share Improve this question edited May 23, 2017 at 10:32 CommunityBot 11 silver badge asked Jul 23, 2010 at 6:31 RyanP13RyanP13 7,78328 gold badges102 silver badges172 bronze badges 3
  • Either of the solutions below would work for you, so I would advise you to go with the one that's less likely to break in the future. The regex option will break if it encounters 2 or more numbers inside the url, while the split option will break if the number is encountered in a different position (hence, ignored). – Ioannis Karadimas Commented Jul 23, 2010 at 7:02
  • @Ioannis Karadimas: More precisely, the regex will match the first number that is enclosed in slashes, i. e. it matches 123 in /foo/123/bar/456/baz. It doesn't just match any number, so it works correctly for strings like /foo123/bar/456/baz and matches 456. – Tim Pietzcker Commented Jul 23, 2010 at 9:11
  • @Tim Pietzcker: Sorry, I did not properly outline the regex's function. However, I was referring to the general case where it will match 123 in /foo/123/bar/456/. If the desired unique number is 456, I think the problem is evident. It all es down to the expected general form of the url. – Ioannis Karadimas Commented Jul 23, 2010 at 9:37
Add a ment  | 

3 Answers 3

Reset to default 6

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]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745194746a4616036.html

相关推荐

  • regex - Extracting number ID from a URL in Javascript - Stack Overflow

    Similar to my previous question:spliting a string in JavascriptThe URLs have now changed and the unique

    1小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信