Aerodynamika
Aerodynamika

Reputation: 8413

How to remove URL from a string completely in Javascript?

I have a string that may contain several url links (http or https). I need a script that would remove all those URLs from the string completely and return that same string without them.

I tried so far:

 var url = "and I said http://fdsadfs.com/dasfsdadf/afsdasf.html";
 var protomatch = /(https?|ftp):\/\//; // NB: not '.*'
 var b = url.replace(protomatch, '');
 console.log(b);

but this only removes the http part and keeps the link.

How to write the right regex that it would remove everything that follows http and also detect several links in the string?

Thank you so much!

Upvotes: 19

Views: 27874

Answers (2)

anubhava
anubhava

Reputation: 784888

You can use this regex:

var b = url.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '');
//=> and I said 

This regex matches and removes any URL that starts with http:// or https:// or ftp:// and matches up to next space character OR end of input. [\n\S]+ will match across multi lines as well.

Upvotes: 47

Kyle
Kyle

Reputation: 1443

Did you search for a url parser regex? This question has a few comprehensive answers Getting parts of a URL (Regex)

That said, if you want something much simpler (and maybe not as perfect), you should remember to capture the entire url string and not just the protocol.

Something like /(https?|ftp):\/\/[\.[a-zA-Z0-9\/\-]+/ should work better. Notice that the added half parses the rest of the URL after the protocol.

Upvotes: 1

Related Questions