Given the following Pattern:
ptrn = "url/{test1}/url2/{test2}/url3?param1=¶m2="
and following string
fullURL = "url/abcd/url2/efgh/url3?param1=¶m2="
I want to remove {test1}
and {test2}
from the fullURL
-string, so that the outcome looks like this:
modifiedfullURL = "url//url2//url3?param1=¶m2="
"abcd"
and "efgh"
can be everything else dependent on the call.
Is there any way to do this "clean"?
I tried with regex, but here of course the pattern can always change, so it would be nice if i can identify the strings which needs to be replaced, based on the pattern.
2
Answers
The question is not clear enough. You need to provide the range of possible inputs.
However, based on the provided information, my understanding is you want to remove the items in odd positions. ie, 1st index, 3rd index etc.
If that the case, then it could be done easily using the below methods.
const modifiedUrl = fullUrl.split(‘/’).filter((segment, index)=> index % 2 === 0).join(‘//’);
console.log(modifiedUrl);
Looks like you want to replace certain URL path components with nothing (effectively removing them). Since the components you want to remove are always between slashes ("/"), you can use the following approach:
Split your pattern and URL into components by using the slash as the separator.
Compare each component from the pattern with the corresponding component from the URL. If the pattern component does not contain curly braces ("{" and "}"), copy the URL component to the result. Otherwise, skip it.
Join the result components with slashes.
Here’s how you could implement this in Python:
This will output: "url//url2//url3?param1=¶m2="
This solution assumes that your pattern and URL have the same number of components (i.e., the same number of slashes). If this is not the case, you would need to add additional error handling.