简体   繁体   中英

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM