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);
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.
0 Comments