正則匹配文字中的鏈結

2022-07-14 11:39:22 字數 862 閱讀 1894

最近做的乙個專案中遇到使用正則匹配一段文字中的 url 並將其顯示為短連線的需求。

顯示的文字包括為防止 xss 而用 js 處理過的帶有特殊符號的文字以及 php 直接渲染後的文字,所以需要兩個正規表示式。

//

匹配不帶特殊符號的文字的正則

//匹配帶有特殊符號的文字的正則

//type 為 0 表示文字中不帶有特殊符號

if( type == '0')else

if( type == '1')

var result = txt.replace(reg,function

(item));

return

result;

}

Python 正則匹配文字中的時間串

text 現在是北京時間2020 3 16 2020年3月16日 2020.3.16 13 00 re.findall d年 d月 d日 text 2020年3月16日 re.findall d d d text 2020 3 16 re.findall d 年 d 月 d 日 text 2020 ...

java匹配超連結 正則

網頁中鏈結的正規表示式 string re pattern p pattern.compile re,pattern.case insensitive pattern.dotall pattern.multiline 其中href中的內容可以用單引號也可以用雙引號,還可以不加引號。一下都可以匹配 電...

用正則匹配富文字中的文字,並替換其內容

問題描述 有這樣的一段字串 123 4 5 6 789 helloworld 這是一段包含了 html 標籤資訊的富文字,現在需要將其中的文字內容處理為全大寫 解決方案 function supertrim str const str 123 4 5 6 789 helloworld supertr...