MonkimoE
MonkimoE

Reputation: 115

Removing all url in textarea from variable using jquery

I need to remove all url from variable in textarea. But it only remove the first one. Need to removed at all once the button clicked.

var urlDelete = "https://upload.wikimedia.org/wikipedia/commons/a/a0/Google_favicon_2012.jpg";

$('#remove').click(function() {
  $('#input').val(
    $('#input').val().replace(urlDelete, ''))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<textarea id="input" style="width:400px; height: 120px">
  remove all url from variable: https://upload.wikimedia.org/wikipedia/commons/a/a0/Google_favicon_2012.jpg 
  
  this url must be removed: https://upload.wikimedia.org/wikipedia/commons/a/a0/Google_favicon_2012.jpg 
  
  must not be effected: http://stackoverflow.com
  and http://google.com 
  
  again: https://upload.wikimedia.org/wikipedia/commons/a/a0/Google_favicon_2012.jpg


</textarea>
<br/>
<br/>
<button id="remove">remove</button>

How the best way to make all url removed?

Thank you so much for any guide.

Upvotes: 0

Views: 656

Answers (4)

Nizar B.
Nizar B.

Reputation: 3118

Textarea is also considered as an input element containing a value. You only need to do:

 <textarea id="input" style="width:400px; height: 200px">

 $(#input).val('');

Ok so want you need to do its first get the content of your textarea, whatever it does contain:

 var textareaContent = $('#url-textarea').val();

Then you can use a regex against your textareaCOntent variable, something similar to this:

      var newTextareaContent = textareaContent.replace((http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#], '')?

)

Then you just append the new content to your textarea:

 textareaContent.val(newTextareaContent);

Upvotes: 0

Hari Ram
Hari Ram

Reputation: 305

Replace will only replace the first occurrence of the string. To replace all the occurrence of the string, you should use a regular expression with the global search. Modify your javaxcript as shown below.

var urlDelete = "https://upload.wikimedia.org/wikipedia/commons/a/a0/Google_favicon_2012.jpg";
var regularExp = new RegExp(urlDelete, 'g'); 

$('#remove').click(function() {
  $('#input').val(
    $('#input').val().replace(regularExp, ''))
});

Upvotes: 1

loveNoHate
loveNoHate

Reputation: 1547

You must use the global flag.

var urlDelete = /https:\/\/upload.wikimedia.org\/wikipedia\/commons\/a\/a0\/Google_favicon_2012.jpg/g;

$('#remove').click(function() {
    $('#input').val(
    $('#input').val().replace(urlDelete, ''))
});

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Using_global_and_ignore_with_replace()

Upvotes: 0

Ajay Narain Mathur
Ajay Narain Mathur

Reputation: 5466

You can split the at particular string and then join the split ed array:

var urlDelete = "https://upload.wikimedia.org/wikipedia/commons/a/a0/Google_favicon_2012.jpg";

$('#remove').click(function() {
  $('#input').val(
    $('#input').val().split().join(urlDelete);
});

Check this snippet

var urlDelete = "https://upload.wikimedia.org/wikipedia/commons/a/a0/Google_favicon_2012.jpg";

$('#remove').click(function() {
  $('#input').val(
    $('#input').val().split(urlDelete).join(""))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<textarea id="input" style="width:400px; height: 120px">
  remove all url from variable: https://upload.wikimedia.org/wikipedia/commons/a/a0/Google_favicon_2012.jpg this url must be removed: https://upload.wikimedia.org/wikipedia/commons/a/a0/Google_favicon_2012.jpg must not be effected: http://stackoverflow.com
  and http://google.com again: https://upload.wikimedia.org/wikipedia/commons/a/a0/Google_favicon_2012.jpg


</textarea>
<br/>
<br/>
<button id="remove">remove</button>

Upvotes: 2

Related Questions