0

I want to fetch url links from textbox in asp.net application
I have textarea when I entered text if it contains links like www.google.com or http://google.com then I want get every links or first link from textarea value.
How I achieve with Jquery or Javascript language. I want to get each or first links when I typing words/text into textarea control. here, I writes some code.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test Page </title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <div class="innerWrap">
       <textarea autocomplete="off" id="txtUpdate" style="height: 48px; width: 408px;" > 
          </textarea>
     </div>
</div>
<script>
$(document).ready(function(){
    $('#txtUpdate').keyup(function() {    
        getURLfromText(jQuery.trim($(this).val()));
    });     
});
function getURLfromText(objTxt)
{
  // code
}
</script>
 </form>
</body>
</html>

Please give me correct solution for that, also should be works I copied and paste some text. How It possible.?

Edited

Input

Down to Earth: Tracy Caldwell Dyson www.google.com
After spending nearly six months in space,
she is back on the ground, with two expeditions
under her belt. http‍://bit‍.ly/dEpNHo

OutPut

Array linkarray[]=new Array(); linkarray[0]="www.google.com"; linkarray[1]="http://‍bit.ly/dEpNHo";

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Abhishek B.
  • 5,112
  • 14
  • 51
  • 90

1 Answers1

1

I've posted a short example on jsfiddle. I think this is the kind of thing you're looking for? The regex for urls included probably accepts a lot of values you do not want. You can switch that for an expression you prefer.

The script basically takes the value of the textarea, splits it on all whitespace, and then tests each value of the array to see whether it is a url using regex.

http://jsfiddle.net/UkCPq/1/

HTML:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Test Page </title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div class="innerWrap">
                <textarea autocomplete="off" id="txtUpdate" style="height: 48px; width: 408px;" ></textarea>
            </div>
        </form>
        <p>Your urls, sir:</p>
        <ul id="theurls">
            <li>:)</li>
        </ul>
    </body>
</html>

Javacript:

$(document).ready(function() {
    $('#txtUpdate').keyup(function() {
        getURLfromText($.trim($(this).val()));
    });
});

function getURLfromText(objTxt) {
    var urlpatt = new RegExp(/[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi);
    var objArray = objTxt.split(/\s/g);
    $('#theurls').text('');
    $.each(objArray, function(key, value) {
        var result = value.match(urlpatt);
        if (result !== null) {
            $('<li>' + result + '</li>').appendTo('#theurls');
        }
    });
}
mjadobson
  • 83
  • 7
  • When I added url with "www.google.com" then it did not work please give me code of url with "www" should work ... – Abhishek B. Mar 23 '11 at 11:48
  • above code work well with http://abcd.com links it did not work if link is like www.abcd.com. Can give code which also work with 'www' extension? – Abhishek B. Mar 23 '11 at 12:07
  • I've replaced the regular expression with the one found here in both my answer and my jsfiddle: http://stackoverflow.com/questions/3809401/javascript-url-regex It should now give 'www.google.com', 'abcd.co.uk', etc. – mjadobson Mar 23 '11 at 13:05