skip to Main Content

Given the following Pattern:

ptrn = "url/{test1}/url2/{test2}/url3?param1=&param2="

and following string

fullURL = "url/abcd/url2/efgh/url3?param1=&param2="

I want to remove {test1} and {test2} from the fullURL-string, so that the outcome looks like this:

modifiedfullURL = "url//url2//url3?param1=&param2="

"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


  1. 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 test1 = 'abcd';
    const test2 = 'efgh'
    const fullUrl = `url/${test1}/url2/${test2}/url3?param1=&param2=`
    
    const modifiedUrl = fullUrl.split('/').filter((_, index)=> index % 2 === 0).join('//');
    console.log("fullUrl", "=>", fullUrl);
    console.log("modifiedUrl", "=>", modifiedUrl);

    const modifiedUrl = fullUrl.split(‘/’).filter((segment, index)=> index % 2 === 0).join(‘//’);
    console.log(modifiedUrl);

    Login or Signup to reply.
  2. 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:

    def modify_url(pattern, url):
        pattern_components = pattern.split('/')
        url_components = url.split('/')
        result_components = []
    
        for pattern_component, url_component in zip(pattern_components, url_components):
            if '{' not in pattern_component and '}' not in pattern_component:
                result_components.append(url_component)
    
        return '/'.join(result_components)
    
    pattern = "url/{test1}/url2/{test2}/url3?param1=&param2="
    fullURL = "url/abcd/url2/efgh/url3?param1=&param2="
    modifiedfullURL = modify_url(pattern, fullURL)
    
    print(modifiedfullURL)
    

    This will output: "url//url2//url3?param1=&param2="

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search