0

I have a url like

http://www.blah.com/something/maybesomethingelse/Webservices/something.asmx/blah

That is being passed through a jquery ajax request.

I want to remove everything after /Webservices/ so I can stick a new page in for error handling.

so ideally this would return

http://www.blah.com/something/maybesomethingelse/Webservices/

Then i could just concat on the new page.

Thanks for your help :)

4
  • 4
    Simple way: url = url.split(/\/Webservices\/)[0]+"/Webservices/"+yourstuff Commented Mar 23, 2011 at 14:16
  • You want to do this in Javascript? Commented Mar 23, 2011 at 14:16
  • @mplungjan: that comment seems worthy of being an answer +1 for the comment, and an up-vote on your answer (should you post it) =] Commented Mar 23, 2011 at 14:30
  • @David: Thanks :) I was sure that if I posted it, some jQuery or RegEx purists would downvote it as far as they could ;) Commented Mar 23, 2011 at 14:36

2 Answers 2

2

Simple way: url = url.split(/\/Webservices\/)[0]+"/Webservices/"+yourstuff

or neater

var lastFolder = "/Webservices/";
url = url.split(lastFolder)[0]+lastFolder+yourstuff;
Sign up to request clarification or add additional context in comments.

Comments

1

You could use a parser like this one:

http://stevenlevithan.com/demo/parseuri/js/

but in this simple case, you only need to find the last index of "/Webservices/":

var lastIndex = url.lastIndexOf('/Webservices/')

and then take everything from the beginning up to the end of "/Webservices/":

var baseUrl = url.substring(0, lastIndex + 12)

and then append the new page:

var newUrl = baseUrl + '/Error.aspx'

Comments

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.