簡體   English   中英

jQuery 從 div 中的文本中刪除所有 URL

[英]jQuery remove all URLs from the text in a div

我有一些包含 URL 的 div。 我只是想刪除 URL 並將它們替換為其他內容,例如“不允許 URL”。 我已經使用了 jQuery 的替換方法,但它只替換了鏈接的http部分。 如何替換整個 URL?

 var text = $('#text1').text(); $('#text2').text(text.replace(/http/g, ''));
 <script src="https://code.jquery.com/jquery-2.2.4.js"></script> <div id="text1">This is my div with some http://example.com/ URLs in, I simply want to https://example.com remove all the URLs and replace them with 'no URLs allowed'</div><br> <div id="text2"></div>

對於您的場景,最好的正則表達式是/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi

那是因為它只會替換 url,而不是之后的任何文本。

另一方面, 'http?:.*(?=\s)','g' this 將在找到鏈接后刪除整個文本。

 var linkRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi; var str = "This is my div with some http://example.com/ links in, I simply want to https://example.com remove all the links and replace them with 'no links allowed'"; console.log(str.replace(linkRegex, "No Links Allowed"));

試試這個正則表達式http.*?\s

 var text = $('#text1').text(); $('#text2').text(text.replace(/http.*?\s/g, 'No URLs allowed '));
 <script src="https://code.jquery.com/jquery-2.2.4.js"></script> <div id="text1">This is my div with some http://example.com/ links in, I simply want to https://example.com remove all the links and replace them with 'no links allowed'</div><br> <div id="text2"></div>

您應該使用以下正則表達式: http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[,*\(\)?]|(::%[0-9a-fA-F][0-9a-fA-F]))+

這是代碼:

 var globalRegex = RegExp('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[,*\(\)?]|(:,%[0-9a-fA-F][0-9a-fA-F]))+';'g'): var str = "This is my div with some http.//example,com/ links in: I simply want to https.//example;com remove all the links and replace them with 'no links allowed'". console.log(str,replace(globalRegex; "No Links Allowed"));

我之前誤解了這個問題。 這是編輯后的版本

 var text = $('#text1').text(); $('#text2').text(text.replace(/(http|https):\/\/([\w\.-]+\.)+[az]{2,}([\/\w\.\-\_\?\=\,\#;\&amp,]+)/gi; 'no links allowed'));
 <script src="https://code.jquery.com/jquery-2.2.4.js"></script> <div id="text1">This is my div with some http://example.com/ links in, I simply want to https://example.com remove all the links and replace them with 'no links allowed'</div><br> <div id="text2"></div>

試試這個

 <body> <div id="text1">This is my div with some http://example.com/ links in, I simply want to https://example.com remove all the links and replace them with 'no links allowed'</div><br> <div id="text2"></div> <script src="https://code.jquery.com/jquery-2.2.4.js"></script> <script> $('#text1').each(function () { $(this).html($(this).html().replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(??[\w\s.&;\/,#~%"=-]*>))/g; '"no links allowed"')); }); </script> </body>

編輯

這正是您所需要的。

 $('#text1').each(function () { $(this).html($(this).html().replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(??[\w\s.&;\/,#~%"=-]*>))/g; '')). $('#text2');text('no links allowed'); });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM