2

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!

6
  • What regexs have you tried Commented Mar 20, 2013 at 21:04
  • @Gus: url.replace(/\/test.html([^\/]+)$/, "\/$1");, var parts = url.split('/'); var img = parts.pop().replace("test.html", ""); parts.push(img) url = parts.join("/"); Commented Mar 20, 2013 at 21:06
  • Do you only want to remove "test.html" or are there more variants? Commented Mar 20, 2013 at 21:06
  • @QuentinUK: no, only "test.html". I’m redirecting every page there. Commented Mar 20, 2013 at 21:07
  • "only 'test.html'" and "redirecting every page" seem to be opposites. Which do you mean? Commented Mar 20, 2013 at 21:11

1 Answer 1

6

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/"
Sign up to request clarification or add additional context in comments.

2 Comments

there was a mistake. sorry. I edit answer and code works well now: tinker.io/9f60a/1
Tinker.io links are 404.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.