How to Extract URLs from a String in JavaScript ? Last Updated : 29 May, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we are given a string str which consists of a hyperlink or URL present in it. We need to extract the URL from the string using JavaScript. The URL must be complete and you need to print the entire URL. Example: Input :-str = "Platform for geeks: https://www.geeksforgeeks.org"Output :-https://www.geeksforgeeks.orgExplanation: In the input string "https://www.geeksForgeeks.org" is a URL and we are required to extract this URL.Approaches to extract URLs from a string in JavaScript: Table of Content Approach 1: Using Regular ExpressionsApproach 2: Using the split() MethodApproach 3: Using the URL Constructor Approach 1: Using Regular ExpressionsIn this approach, we use the match method to find the first occurrence of a URL in the string. The regular expression `/https?:\/\/[^\s]+/` matches any string that starts with http or https, followed by one or more non-space characters. The [0] at the end of the match method returns the first match which is the entire URL present in the string. Example: Below is the implementation of this approach JavaScript let str = "Platform for geeks: https://www.geeksforgeeks.org"; let res = str.match(/https?:\/\/[^\s]+/)[0]; console.log("The extracted URL from given string is:- " + res); OutputThe extracted URL from given string is:- https://www.geeksforgeeks.org Approach 2: Using the split() MethodIn this approach, we use the split() method to split the string into an array of words and then we use the find method to find the first word that starts with http or https. This approach assumes that the URL is separated from the rest of the string by whitespaces meaning that there shouldn't be any whitespaces in the URL . Example: Below is the implementation of this approach JavaScript let str = "Platform for geeks: https://www.geeksforgeeks.org"; let res = str.split(" ").find(word => word.startsWith("http")); console.log("The extracted URL from given string is: " + res); OutputThe extracted URL from given string is: https://www.geeksforgeeks.org Approach 3: Using the URL ConstructorIn this approach, we iterate over each word in the string, attempt to create a URL object, and check if it is a valid URL. This method is more robust as it explicitly verifies the validity of the URL. Example: Below is the implementation of this approach: JavaScript let str = "Platform for geeks https://www.geeksforgeeks.org"; let words = str.split(/\s+/); // Using a regex to split by whitespace let url = null; // Initialize url to null for (let word of words) { try { let potentialUrl = new URL(word); url = potentialUrl.href; break; } catch (e) { // If the word is not a valid URL, it will throw an error which we ignore } } if (url !== null) { console.log("The extracted URL from given string is: " + url); } else { console.log("No URL found in the given string."); } OutputThe extracted URL from given string is: https://www.geeksforgeeks.org/ Comment More infoAdvertise with us Next Article How to Extract URLs from a String in JavaScript ? A adityajoh8d3r Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Program +1 More Similar Reads JavaScript - Extract First Word from a String Here are the different methods to extract the first word from a stringUsing split() Method - Most PopularThe split() method splits the string into an array of words based on spaces, and the first element of this array is the first word.JavaScriptconst s1 = "Hello World, welcome to GeeksforGeeks"; co 2 min read How to Check if a String Contains a Valid URL Format in JavaScript ? A string containing a valid URL format adheres to standard conventions, comprising a scheme (e.g., "https://" or "https://"), domain, and optionally, a path, query parameters, and fragments. Ensuring this format is crucial for data consistency and accurate handling of URLs in applications.There are s 2 min read How to Convert Camel Case String to Snake Case in JavaScript ? We are going to learn about the conversion of camel case string into snake case by using JavaScript. Camel case string means words combined, each starting with uppercase and Snake case means words joined with underscores, all lowercase, Converting camel case to snake case involves transforming strin 3 min read JavaScript- Remove Last Characters from JS String These are the following ways to remove first and last characters from a String:1. Using String slice() MethodThe slice() method returns a part of a string by specifying the start and end indices. To remove the last character, we slice the string from the start (index 0) to the second-to-last charact 2 min read JavaScript Program to Remove Last Character from the String In this article, we will learn how to remove the last character from the string in JavaScript. The string is used to represent the sequence of characters. Now, we will remove the last character from this string. Example: Input : Geeks for geeks Output : Geeks for geek Input : Geeksforgeeks Output : 3 min read JavaScript Program to Extract Strings that contain Digit We are given a Strings List, and the task is to extract those strings that contain at least one digit.Example:Input: test_list = [âgf4gâ, âisâ, âbestâ, âgee1ksâ] Output: [âgf4gâ, âgee1ksâ] Explanation: 4, and 1 are respective digits in a string.Input: test_list = [âgf4gâ, âisâ, âbestâ, âgeeksâ] Outp 5 min read JavaScript Program to get Query String Values Getting query string values in JavaScript refers to extracting parameters from a URL after the "?" symbol. It allows developers to capture user input or state information passed through URLs for dynamic web content and processing within a web application. Table of Content Using URLSearchParamsUsing 2 min read JavaScript Program to Truncate a String to a Certain Length In this article, we are going to learn how can we truncate a string to a certain length. Truncating a string to a certain length in JavaScript means shortening a string so that it doesn't exceed a specified maximum length. This can be useful for displaying text in user interfaces, such as titles or 3 min read JavaScript - Remove all Occurrences of a Character in JS String These are the following ways to remove all occurrence of a character from a given string: 1. Using Regular ExpressionUsing a regular expression, we create a pattern to match all occurrences of a specific character in a string and replace them with an empty string, effectively removing that character 2 min read JavaScript URLify a given string (Replace spaces is %20) In this article, we are going to discuss how can we URLify a given string using JavaScript. In which we will be transforming the spaces within the input strings into the "%20" sequence. The process of URLLification consists of replacing these spaces with the '%20' encoding. This task is essential fo 6 min read Like