javascript

Javascript examples to remove url from a string

If you're looking for a way to remove the url from a string using Javascript, you've come to the right place. In this article, we'll show you a few different examples of how to do this.

const url = "We have https://www.devsheet.com/my-example/ as a url";

const result = url.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '');

console.log("result is: ", result);

Solution 1: Remove URL from string using Regex

You can use Regular expressions to remove a URL from a string. We will use String.replace() function along with regex in order to do that.

Below is an example:

const url = "We have https://www.devsheet.com/my-example/ as a url";

const result = url.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '');

console.log("result is: ", result);

Output

We have  as a url

The replacement function above takes the URL as a string and replaces it with an empty string. This effectively removes the URL from the string.

Was this helpful?