skip to Main Content

Situation:

When an id element selected, get the current URl and change the /en/ part to other language

current URL

http://127.0.0.1:5500/2023/en/activities_summary.html

I want to change it to

http://127.0.0.1:5500/2023/tc/activities_summary.html

code:

    $('#tc').on('click',function(){
           
              
            if(location.href.match(//(?:en|sc)//)){
                location.replace(/^.+?/(?:en|sc)//,'tc') 
            }
        })

2

Answers


  1. You can change the /en/ part this way:

    location.replace(/(/en/)/, '/tc/')
    
    Login or Signup to reply.
  2. (?<=/d{4}/) – your year part (4 digits)
    [a-z]{2} – your language part (2 chars)
    (?=/) – closing slash part

    const url = 'http://127.0.0.1:5500/2023/en/activities_summary.html';
    
    console.log(url.replace(/(?<=/d{4}/)[a-z]{2}(?=/)/, 'tc'));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search