I have the following string
https://test.io/content/storage/id/urn:aaid:sc:US:8eda16d4-baba-4c90-84ca-0f4c215358a1;revision=0?component_id=e62a5567-066d-452a-b147-19d909396132
I need to use regex to get the following string
urn:aaid:sc:US:8eda16d4-baba-4c90-84ca-0f4c215358a1
from the url
above.
The string will always begin with a urn and end with a letter or number.
Can someone please help? How can I do this using typescript?
Here’s what I tried but it gave me an null value. Thanks!
function extractAssetIdFromUrl(url: string) {
// Regular expression to match the desired pattern
const regex = /urn[w-]+/;
// Use the regex to find the match in the URL
const match = url.match(regex);
// Check if there is a match and return it, otherwise return null
return match ? match[0] : null;
}
3
Answers
First extract the last path segment. Then you can match it against your required characters without having to worry about the query string or any other part of the URL
It looks like your id is terminated with a
;
, so I’d assume a regex for "starts withurn:
", and "then everything that isn’t;
" should work just fine:And then, of course,
https://regexr.com is very useful for understanding and deconstructing regex patterns.
This regex will do what you want based on the example pattern provided:
To use this pattern to extract it from the example URL string you provided, you could do this:
As others have said, though, I would recommend first extracting the path out of the URL, as URLs can have things like query strings, which could also contain a pattern that matches this, resulting in possible unintended results.
Also, other have mentioned the fact that there appears to be a semicolon delimiter used in the URL, which could make your work much easier if that will reliably exist in the URL (and possibly even avoid the need for regex).