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.

HTML

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

 <textarea id="textid" rows="10" cols="40">https://www.neukanndo.com/javascript/allow-numeric-value-with-dot-only-using-javascript.html  Allow only numbers to be typed in a textbox i.e. numeric value or digits in TextBox using JavaScript https://www.neukanndo.com/angular/how-to-use-sessionstorage-in-angular-js.html SessionStorage is similar to localstorage, except that data is stored in sessionstorage but it expired.</textarea>

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

JAVASCRIPT

<script>
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>");
}
</script>

0 Comments

Leave a Replay

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

>