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?
Similar Posts
- While loop in Javascript with examples
- Validate a url using regex in Javascript
- Util Array Examples
- allows keeping the URL while rendering a different page
- Remove last character of a string in Javascript
- Remove new line separator from start and end positions of string in Javascript
- Remove all whitespaces from a string using javascript