How to replace plain URL with link using JavaScript ? Last Updated : 02 Aug, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a plane URL, the task is to replace the plain URLs with the links. This problem can be solved with the help of Regular Expressions. Approach: Using RegExp and replace() MethodRegExp - This looks for the URL in the provided text.This RegExp parses the URL and puts the address to the $1 variable.After getting the URL in the text, it replaces it with <a> element and sets its href and other attributes.Example: This example implements the above approach. html <!DOCTYPE HTML> <html> <head> <title> How to replace plain URL with link using JavaScript ? </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="font-size: 24px; font-weight: bold; color: green;"> </p> <script> let up = document.getElementById('GFG_UP'); let down = document.getElementById('GFG_DOWN'); let text = 'This is https://www.geeksforgeeks.org/'; up.innerHTML = "Click on the button to replace " + "plain URLs with links.<br>" + text; function rep(text) { // Put the URL to variable $1 after visiting the URL const Rexp = /((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g; // Replace the RegExp content by HTML element return text.replace(Rexp, "<a href='$1' target='_blank'>Link to URL</a>"); } function GFG_Fun() { down.innerHTML = rep(text); } </script> </body> </html> Output: Example 2: In this example, we will put the URL to variable $1 and the Domain name to $3 after visiting the URL html <!DOCTYPE HTML> <html> <head> <title> How to replace plain URL with link using JavaScript ? </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="font-size: 24px; font-weight: bold; color: green;"> </p> <script> let up = document.getElementById('GFG_UP'); let down = document.getElementById('GFG_DOWN'); let text = 'This is https://www.geeksforgeeks.org/'; up.innerHTML = "Click on the button to replace " + "plain URLs with links.<br>" + text; function rep(text) { // Put the URL to variable $1 and Domain name // to $3 after visiting the URL const Rexp = /(\b(https?|ftp|file):\/\/([-A-Z0-9+&@#%?=~_|!:,.;]*)([-A-Z0-9+&@#%?\/=~_|!:,.;]*)[-A-Z0-9+&@#\/%=~_|])/ig; // Replacing the RegExp content by HTML element return text.replace(Rexp, "<a href='$1' target='_blank'>$3</a>"); } function GFG_Fun() { down.innerHTML = rep(text); } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to parse URL using JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to modify URL without reloading the page using JavaScript ? In this article, we are going to see how to modify URL without reloading the page using JavaScript Below are the methods to modify the URL without reloading the page using JavaScript: Table of Content Replacing the current state with replaceState() MethodAdding a new state with pushState() MethodMet 3 min read How to parse URL using JavaScript ? Given an URL and the task is to parse that URL and retrieve all the related data using JavaScript. Example: URL: https://www.geeksforgeeks.org/courses When we parse the above URL then we can find hostname: geeksforgeeks.com path: /courses Method 1: In this method, we will use createElement() method 2 min read How to Open URL in New Tab using JavaScript? To open a URL in a new tab using JavaScript, we can use the window.open() method. The window.open() method is used to open a new browser window or tab. It can also be used to load a specific URL in a new tab. Syntaxwindow.open(URL, '_blank');window.open(): Opens a new tab or window.First Parameter: 3 min read How to open URL in a new window using JavaScript? In HTML, the anchor tag (<a>) is used to open new windows and tabs in a very straightforward manner. However, there are situations where you need to achieve the same functionality using JavaScript. This is where the window.open() method becomes useful.The window.open() method is used to open a 3 min read How to get URL Parameters using JavaScript ? To get URL parameters using JavaScript means extracting the query string values from a URL. URL parameters, found after the ? in a URL, pass data like search terms or user information. JavaScript can parse these parameters, allowing you to programmatically access or manipulate their values.For getti 3 min read How to Open a Link Without Clicking on it using JavaScript? To open a link without clicking on it we can use the onmouseover method of JavaScript. The link will open when the mouse moves over the text. It returns a newly created window, or NULL if the call gets failed. Syntax:window.open( URL, name, Specs )Note: Allow Pop-up of Web Browser. Example 1: URL is 1 min read Like