skip to Main Content

I have a RegEx originally like this:

/https://medal.tv/gameseuro-truck-sim-2/clips/([A-z0-9-]+)(/[A-z0-9]+)?(??[A-z0-9]+=?[A-z0-9]+-[A-z0-9]+)?/

which works perfectly, however, I would like it to work both when it is "euro-truck-sim-2" and "imported-clips". I have not too much (practically, zero) experience with RegEx and I have no clue what I am doing wrong. This is what I tried and I don’t know if the RegEx or something else is broken with the code itself.

/https://medal.tv/games/(imported-clips|euro-truck-sim-2+)?/clips/([A-z0-9-]+)(/[A-z0-9]+)?(??[A-z0-9]+=?[A-z0-9]+-[A-z0-9]+)?/

What I see is that after the imported-clips match, there is nothing else after that in my link:

https://medal.tv/clips/imported-clips

Expected behavior should be:

https://medal.tv/games/imported-clips/clips/remglmZK54/egmkr4t

or

https://medal.tv/games/euro-truck-sim-2/clips/remglmZK54/egmkr4t

changed into

https://medal.tv/clips/remglmZK54

2

Answers


  1. Chosen as BEST ANSWER

    Found the solution:

    
    /https://medal.tv/games/(?:imported-clips|euro-truck-sim-2)/clips/([A-z0-9-]+)(/[A-z0-9]+)?(??[A-z0-9]+=?[A-z0-9]+-[A-z0-9]+)?/
    
    

  2. const str1 = 'https://medal.tv/games/imported-clips/clips/remglmZK54/egmkr4t';
    const str2 = 'https://medal.tv/games/euro-truck-sim-2/clips/remglmZK54/egmkr4t';
    
    let re =
        /(?<=https://medal.tv/)games/(imported-clips|euro-truck-sim-2)/(?=clips/[A-z0-9]+)|/[A-z0-9]+$/g;
    
    let out1 = str1.replace(re, '');
    console.log(out1)
    
    let out2 = str2.replace(re, '');
    console.log(out2)
    

    prints

    https://medal.tv/clips/remglmZK54
    https://medal.tv/clips/remglmZK54
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search