1

I have a URL like this-

http‍://localhost/gtwhero/public/users/13/subscribers-lists/30/subscribers

So it's format is like this-

http‍://localhost/gtwhero/public/users/{user_id}/subscribers-lists/{list_id}/subscribers

I want to get the user_id and list_id from the URL.

So what I have done is

var a = parseInt("http://localhost/gtwhero/public/users/13/subscribers-lists/30/subscribers");

But as a result I am getting -

NaN

So, I think there must be another way of getting those two integers.

Can anyone please help?

1 Answer 1

1

parseInt wont work in this case, it'll always return NaN as your string contains alphabets.

You need to use regex to extract the user id from URL

var url = 'http://localhost/gtwhero/public/users/13/subscribers-lists/30/subscribers';
var data = (url.match(/(\d+)/g) || []);

var userId = data[0],
  listId = data[1];

document.write("userId " + userId);
document.write("<br />listId " + listId);

Note: this will only work specifically for the above shown URLs, if URL contains other digits, it need to be used properly to extract intended ids.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.