Replace part of URL with JavaScript

12,389

Use replace() function

var url = 'http://website.com/test.html/post/something-else/'
url = url.replace('/test.html','')
console.log(url) // "http://website.com/post/something-else/"
Share:
12,389
Alex
Author by

Alex

Updated on July 26, 2022

Comments

  • Alex
    Alex almost 2 years

    I want to replace part of the URL with JavaScript but stupid regex is giving me headaches. I want to switch from this:

    http://website.com/test.html/post/something-else/
    

    to this:

    http://website.com/post/something-else/
    

    Any ideas? Thanks!