[英]JS - how to replace in regex for links
我有一個正則表達式替換了從 http 到[]
中某些字符出現的鏈接,我想添加
到這些字符 - 即用某些字符或硬空格的出現替換字符串:
"https://test.pl'".replace(/https?:\/\/[^ "'><]+/g," ")
對於[]
中提到的字符工作正常,我不知道如何在這里添加 
;
您可以使用
.replace(/https?:\/\/.*?(?: |[ '"><]|$)/g," ")
請參閱正則表達式演示。
詳情:
https?:\/\/
- http://
或https://
.*?
- 盡可能少的換行字符以外的任何零個或多個字符(?: |[ '"><]|$)
- 以下之一:
-
字符序列|
- 或者[ "'><]
- 空格、 "
、 '
、 >
或<
字符|
- 或者$
- 字符串結尾。JavaScript 演示:
const texts = ["https://test.pl test","https://test.pl'test"]; const re = /https?:\/\/.*?(?: |[ '"><]|$)/g; for (const s of texts) { console.log(s, '=>', s.replace(re, " ")); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.