簡體   English   中英

JS - 如何在正則表達式中替換鏈接

[英]JS - how to replace   in regex for links

我有一個正則表達式替換了從 http 到[]中某些字符出現的鏈接,我想添加  到這些字符 - 即用某些字符或硬空格的出現替換字符串:

"https://test.pl'".replace(/https?:\/\/[^ "'><]+/g," ")

對於[]中提到的字符工作正常,我不知道如何在這里添加&nbsp ;

您可以使用

.replace(/https?:\/\/.*?(?:&nbsp;|[ '"><]|$)/g," ")

請參閱正則表達式演示

詳情

  • https?:\/\/ - http://https://
  • .*? - 盡可能少的換行字符以外的任何零個或多個字符
  • (?:&nbsp;|[ '"><]|$) - 以下之一:
    • &nbsp; - &nbsp; 字符序列
    • | - 或者
    • [ "'><] - 空格、 "'><字符
    • | - 或者
    • $ - 字符串結尾。

JavaScript 演示:

 const texts = ["https://test.pl&nbsp;test","https://test.pl'test"]; const re = /https?:\/\/.*?(?:&nbsp;|[ '"><]|$)/g; for (const s of texts) { console.log(s, '=>', s.replace(re, " ")); }

暫無
暫無

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

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