CR47
CR47

Reputation: 923

Why is my regex not replacing my string in jQuery?

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

Upvotes: 0

Views: 54

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

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

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

Demo: Fiddle

Upvotes: 4

Related Questions