Paste event catch only a url

Hi,
I’m setting up a link preview like fb
you know you can write a message paste
an url and see the preview.
The point is catching a url in a string
so my first thought was for a regex way
but may be it’s too tricky
http://stackoverflow.com/questions/5461702/regex-to-find-url-in-a-text
peep into this other thread looking for a way to get the pasted string
http://stackoverflow.com/questions/2055224/how-to-get-the-value-of-a-field-during-the-paste-event
I ended up with this solution


$('#message_link').bind({
       paste: function() {
            var bPLength = $.trim($('#message_link').val()).length;
            setTimeout(function() {
                var strInput = $.trim($('#message_link').val());
                var aPLength = strInput.length;
                log(bPLength);
                log(aPLength);
                log(strInput.substr(bPLength,aPLength));
               //var xhr = YD.http.linkPreview($.trim($('#message_link').val()));
            }, 0);
       }
    });

and it seems to work but I’d like to know your opinion.

Bye

tidier :slight_smile:


$('#message_link').bind({
       paste: function() {
            var $self = $(this);
            var bPLength = $self.val().length;
            setTimeout(function() {
                var strInput = $self.val();
                var aPLength = strInput.length;
                var pastedLink = $.trim(strInput.substr(bPLength,aPLength));
                log(pastedLink);
                var xhr = YD.http.linkPreview(pastedLink);
            }, 0);
       }
    });