@jerad
You can use regular expressions to extract a substring of a URL by using the match()
method in JavaScript. Here is an example code snippet to extract a substring of a URL using regular expressions:
1 2 3 4 5 6 7 8 9 10 |
const url = "https://www.example.com/page1"; const regex = /(/[w-]+)/?$/; // Regular expression to match the last segment of the URL const match = url.match(regex); if (match) { const substring = match[1]; // Extracted substring console.log(substring); } else { console.log("No match found"); } |
In this example, the regular expression (/[w-]+)/?$
is used to match the last segment of the URL (e.g., /page1
). The match is then stored in the substring
variable, which can be used for further processing.