JS過濾HTML標籤

2021-07-31 03:13:51 字數 425 閱讀 8993

在前端開發中,我們很有必要過濾掉使用者輸入的文字中的html標籤以防範xss攻擊。我們可以用正規表示式來做到這個:

var filterhtmltag = function (msg)
可能還會需要驗證字串中是否存在某個子字串並獲取子字串,那麼我們可以用indexof()這個方法,如果返貨的下標大於0,那麼子字串就存在,這時候就可以用substr()來獲取子字串了。

例如:

mystr.substr(mystr.indexof(keyword), keyword.length);
另附上我的乙個小demo:

js過濾HTML標籤

function removehtmltag str 第乙個 g 在js中正規表示式是以 開頭的,後面的 g,含義是表示全域性模式,意思是在將匹配的模式應用於整個字串,而不是在第一次匹配上之後就停止匹配了。這個分開來解釋,其中第二個字元 是乙個轉移字元,用來轉移後面的 字元的。匹配0或1個正好在它之...

C 過濾html標籤

c 過濾html標籤 在專案中遇到這樣乙個需求,需要將一段html轉換為一般文字返回,萬能的正規表示式來了。正規表示式來拯救你,如下 1 public static string html2text string htmlstr 23 1213 string regex style s s 定義st...

java過濾html標籤函式

public static string html2text string inputstring string regex style s style s s s s style s 定義style的正規表示式 string regex html 定義html標籤的正規表示式 p script p...