Edit: Please use the code below (a string containing one URL).
$(function(){ var urlmatch = /(https?:\/\/)?([\da-z\.-]+)\.([az\.]{2,6})([\/\w \.-]*)*\/?/gm, protomatch = /\((https?:\/\/)?([\da-z\.-]+)\.([az\.]{2,6})([\/\w \.-]*)*\/?\)/gm; $("#txtarea").keyup(function(){ if( this.value.match(protomatch) && this.value.charAt(0) == '(' && this.value.charAt(this.value.length-1) == ')' ){ this.value = this.value; } else if(this.value.match(urlmatch) && !this.value.match(protomatch) && this.value.charAt(0) != '(' && this.value.charAt(this.value.length-1) != ')'){ this.value = this.value.replace(urlmatch, ''); } }) });
Edit: It will check and delete all the closest URLs (multiple URLs) that go along the entire line.
$(function(){ $("#txtarea").keyup(function(){ var urlmatch = /^(https?:\/\/)?([\da-z\.-]+)\.([az\.]{2,6})([\/\w \.-]*)*\/?$/, newA = [], a = this.value.split(' '); $.each(a, function (i, j) {
Find an updated fiddle for this. Hooray!
source share