Edit: Just to be clear, my question is: How to replace the first url with the paste one if the input has already a url as it's value?
I want to replace the url string if the user pastes a string that contains url (must start with a url). On document load, the input has a url already as it's value. For example: http://www.stackoverflow.com/questions.
If user copy string has a url in it, replace the first url string from input.
For example: https://stackoverflow.com/questions/ask
We will replace the first one with the user paste.
I'v done that using code below but nor working as i want.
$(document).on('paste', 'input.link', function(){
var $element = $(this);
setTimeout(function(){
var $val = $element.val();
var $exp = /(https?:\/\/(?:w{3}\.)?stackoverflow\.com\/?)+/g;
if($val.match($exp)){
var $count = 0;
$val = $val.replace($exp, function(match) {
$count++;
if($count > 1) {
return match;
} else {
return '';
}
});
$element.val($val);
}
}, 100);
});