0

I'm trying to get rid of the protocol and domain from a URL in jQuery with .replace() and regex, but it leaves the string exactly the same no matter what.

var selectedDocumentUrl = "http://mysite.test.com/files/somefile.pdf";
var assetUrl = selectedDocumentUrl.replace('/http://[^\/]+/g', '');

Here is a jsfiddle of the code

1 Answer 1

4

You have to pass a regular expression to replace() not a string literal

var assetUrl = selectedDocumentUrl.replace(/http:\/\/[^\/]+/g, '');

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this worked. Thank you for also pointing out in the code that I had to escape the slashes in the protocol.

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.