-
匹配所有標簽
</?[a-zA-Z! ="-\d]*>
-
匹配閉合標簽以及里面內容
<[a-zA-Z! ="-\d]*>[^</>]*</[a-zA-Z! ="-\d]*>
java使用要對-
進行轉義:
</?[a-zA-Z! ="\-\d]*>
<[a-zA-Z! ="\-\d]*>[^</>]*</[a-zA-Z! ="\-\d]*>
/**
* @author linyufeng.
* @date 2021/2/3 13:34
**/
public class TextUtil {
// 去除html標簽
public static String disHtml(String str) {
return str.replaceAll("</?[a-zA-Z! =\"\\-\\d]*>", "");
}
// 去除html標簽以及里面內容
public static String disAllHtml(String str) {
return str.replaceAll("<[a-zA-Z! =\"\\-\\d]*>[^</>]*</[a-zA-Z! =\"\\-\\d]*>", "");
}
}
((?!abc).)*
否定向前語法, 可以幫助我們去除指定前綴的字符串;[^abc]
范圍比較大,不能起到只過濾abc
的目的;
所以,上述優化格式為: <[a-zA-Z! ="\-\d]*>((?!</).)*</[a-zA-Z! ="\-\d]*>