Languages
[Edit]
EN

JavaScript - get id from URL

5 points
Created by:
Reilly-Collier
860

In this article, we would like to show you how to get id from URL using JavaScript.

The id in the URL can be stored in several ways:

  1. in the path,
  2. in the name,
  3. after the question mark in (request parameter).

In all the solutions below, the main concept is to get the number using the regex groups:

  • (\d+) if the id is a number,
  • ([a-z]+) if the id is a text.

ID in the path

Edit

Number based IDs

Edit

String based IDs

Edit

 

ID in the name

Edit

Number based IDs

Edit

Note:

In this case the ?: in the regular expression cancels the first group, that's why we still can get the id using index 1 of the match.

String based IDs

Edit

ID in request parameter

Edit

Number based IDs

Edit

String based IDs

Edit

References

Edit
  1. Groups and ranges - JavaScript | MDN
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join