Detect URL in Textarea with JavaScript

Detect URL in Textarea with JavaScript

To detect URL from a given text in a textarea and convert it into anchor element and display it on the web page.


<button onclick="replaceURLWithHTMLLinks()">Try it</button>

 <textarea id="textid" rows="10" cols="40">  Allow only numbers to be typed in a textbox i.e. numeric value or digits in TextBox using JavaScript SessionStorage is similar to localstorage, except that data is stored in sessionstorage but it expired.</textarea>

 <p id="demo"></p>


function replaceURLWithHTMLLinks () {
var textid = document.getElementById("textid").value;
var exp = /((https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])/ig;
document.getElementById("demo").innerHTML = textid.replace(exp,"<a href='$1'>$1</a>");


Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed