Reputation: 115
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
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\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#], '')?
)
Then you just append the new content to your textarea:
textareaContent.val(newTextareaContent);
Upvotes: 0
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
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, ''))
});
Upvotes: 0
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