0

url: http://xxxxxx.com/video/view/12345

Can I take 12345 in the url using javascript?

Please help me

1
  • what's your criteria? will it be always after /view? will it be always the last part of the path? Commented Mar 21, 2017 at 16:10

3 Answers 3

1

Use RegExp, Array#match and negative lookahead.

var str = 'http://xxxxxx.com/video/view/12345';
    console.log(str.match(/(?!view\/)\d+/)[0]);

Sign up to request clarification or add additional context in comments.

9 Comments

Duplicates all around. Perhaps find one and close?
@mplungjan There's no exact the same solution as mine, in the link you have provided. Also it's much easier than the methods mentioned there.
You mean this is harder? window.location.href.split('/').pop() - also you assume there is a view always - the duplicate just takes the last
@mplungjan Fine, but your solution will work only if that number is the last element in the url.
And yours only works if view is one but last - more versions here to play with stackoverflow.com/questions/736513/…
|
0

You can also try this if you're sure that it'll always be in last:

var num = location.pathname.split('/').pop(); // "12345"

and further: parseInt(num);

Comments

0

You can parse your URL with the following code. Then just get the last part.

var url = 'http://xxxxxx.com/video/view/12345';
var url_parts = url.replace(/\/\s*$/,'').split('/');
console.log(url_parts[url_parts.length - 1]); // last part

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.