How to remove URLs not copied in brackets from <textarea>

The code in the attached fiddle removes all URLs within the <textarea> .

Is it possible that the code will remove all URLs except that the URL is inside parentheses - for example. (google.com) - dynamically in <textarea> ?

If possible, I would appreciate an updated script as I am new to coding.

 $(function() { $("#txtarea").keyup(function() { console.log(this.value); this.value = this.value.replace(/(https?:\/\/)?([\da-z\.-]+)\.([az\.]{2,6})([\/\w \.-]*)*\/?/mg, ' '); }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <textarea id="txtarea" placeholder="How to stop http being entered into textarea?"></textarea> 

Fiddle

+6
source share
1 answer

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) {//j = $.trim(j); if((j.match(urlmatch) && j.charAt(0) == '(' && j.charAt(j.length-1) == ')') != false) { newA.push(j) } console.log(j, '->', j.match(urlmatch) && j.charAt(0) == '(' && j.charAt(j.length-1) == ')' ) }) var o = newA.join(" ") this.value = o; }) }); 

Find an updated fiddle for this. Hooray!

+1
source

All Articles