0

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.

4 Answers 4

2

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>

Sign up to request clarification or add additional context in comments.

Comments

1

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, ''))
});

Comments

0

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()

Comments

0

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);

1 Comment

Hi Katcha, I need to remove specific word from variable as example in snippet. Not clear all text in textarea. Thank you for your help ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.