使用 jquery 从变量中删除 textarea 中的所有 url

Removing all url in textarea from variable using jquery

我需要从 textarea 中的变量中删除所有 url。 但它只删除第一个。单击按钮后需要完全删除。

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://whosebug.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>

最好的办法是怎样把url全部去掉?

非常感谢您的指导。

您可以 split 特定字符串,然后 join 拆分数组:

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

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

检查这个片段

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://whosebug.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>

您必须使用 global 标志。

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

Textarea 也被视为包含值的输入元素。您只需要做:

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

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

好的,所以你需要首先获取文本区域的内容,无论它包含什么:

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

然后您可以对您的 textareaCOntent 变量使用正则表达式,类似于此:

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

)

然后您只需将新内容附加到文本区域即可:

 textareaContent.val(newTextareaContent);

替换只会替换第一次出现的字符串。要替换所有出现的字符串,您应该使用具有全局搜索功能的正则表达式。如下所示修改您的 javaxcript。

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