程序員必備技能,在java代碼實現替換html標籤,空格,換行符

本文講述了在java代碼裡面是如何實現替換html標籤的實現方式,這也是我們程序員開發必須要掌握的知識,具體代碼:

1 新建replaceHTMLTag方法

這個方法主要是替換傳入參數裡面的html,script,style標籤

/**

* 替換傳入參數裡面的html,script,style標籤

* @param strTmp

* @return

*/

public static String replaceHTMLTag(String strTmp) {

// 腳本的正則表達式

String scriptRegEx = "<script[^>]*?>[\\s\\S]*?<\\/script>";

// 樣式的正則表達式

String styleRegEx = "<style[^>]*?>[\\s\\S]*?<\\/style>";

// 網頁標籤的正則表達式

String htmlRegEx = "<[^>]+>";

Pattern scriptP = Pattern.compile(scriptRegEx, Pattern.CASE_INSENSITIVE);

Matcher scriptM = scriptP.matcher(strTmp);

// 替換script標籤為空白

strTmp = scriptM.replaceAll("");

Pattern styleP = Pattern.compile(styleRegEx, Pattern.CASE_INSENSITIVE);

Matcher styleM = styleP.matcher(strTmp);

// 替換style標籤為空白

strTmp = styleM.replaceAll("");

Pattern htmlP = Pattern.compile(htmlRegEx, Pattern.CASE_INSENSITIVE);

Matcher htmlM = htmlP.matcher(strTmp);

// 替換html標籤為空白

strTmp = htmlM.replaceAll("");

return strTmp.trim(); // 返回文本字符串

}

本文講述了在java代碼裡面是如何實現替換html標籤的實現方式,這也是我們程序員開發必須要掌握的知識,具體代碼:

1 新建replaceHTMLTag方法

這個方法主要是替換傳入參數裡面的html,script,style標籤

/**

* 替換傳入參數裡面的html,script,style標籤

* @param strTmp

* @return

*/

public static String replaceHTMLTag(String strTmp) {

// 腳本的正則表達式

String scriptRegEx = "<script[^>]*?>[\\s\\S]*?<\\/script>";

// 樣式的正則表達式

String styleRegEx = "<style[^>]*?>[\\s\\S]*?<\\/style>";

// 網頁標籤的正則表達式

String htmlRegEx = "<[^>]+>";

Pattern scriptP = Pattern.compile(scriptRegEx, Pattern.CASE_INSENSITIVE);

Matcher scriptM = scriptP.matcher(strTmp);

// 替換script標籤為空白

strTmp = scriptM.replaceAll("");

Pattern styleP = Pattern.compile(styleRegEx, Pattern.CASE_INSENSITIVE);

Matcher styleM = styleP.matcher(strTmp);

// 替換style標籤為空白

strTmp = styleM.replaceAll("");

Pattern htmlP = Pattern.compile(htmlRegEx, Pattern.CASE_INSENSITIVE);

Matcher htmlM = htmlP.matcher(strTmp);

// 替換html標籤為空白

strTmp = htmlM.replaceAll("");

return strTmp.trim(); // 返回文本字符串

}

程序員必備技能,在java代碼實現替換html標籤,空格,換行符

2 新建replaceWrap方法

這個方法主要是替換傳入參數裡面的空格和換行符標籤

/**

* 替換傳入參數裡面的空格和換行符標籤

* @param strTmp

* @return

*/

public static String replaceWrap(String strTmp) {

String retStr = "";

if (strTmp!=null) {

Pattern p = Pattern.compile("\\s*|\t|\r|\n");

Matcher m = p.matcher(strTmp);

retStr = m.replaceAll("");

}

return retStr;

}

本文講述了在java代碼裡面是如何實現替換html標籤的實現方式,這也是我們程序員開發必須要掌握的知識,具體代碼:

1 新建replaceHTMLTag方法

這個方法主要是替換傳入參數裡面的html,script,style標籤

/**

* 替換傳入參數裡面的html,script,style標籤

* @param strTmp

* @return

*/

public static String replaceHTMLTag(String strTmp) {

// 腳本的正則表達式

String scriptRegEx = "<script[^>]*?>[\\s\\S]*?<\\/script>";

// 樣式的正則表達式

String styleRegEx = "<style[^>]*?>[\\s\\S]*?<\\/style>";

// 網頁標籤的正則表達式

String htmlRegEx = "<[^>]+>";

Pattern scriptP = Pattern.compile(scriptRegEx, Pattern.CASE_INSENSITIVE);

Matcher scriptM = scriptP.matcher(strTmp);

// 替換script標籤為空白

strTmp = scriptM.replaceAll("");

Pattern styleP = Pattern.compile(styleRegEx, Pattern.CASE_INSENSITIVE);

Matcher styleM = styleP.matcher(strTmp);

// 替換style標籤為空白

strTmp = styleM.replaceAll("");

Pattern htmlP = Pattern.compile(htmlRegEx, Pattern.CASE_INSENSITIVE);

Matcher htmlM = htmlP.matcher(strTmp);

// 替換html標籤為空白

strTmp = htmlM.replaceAll("");

return strTmp.trim(); // 返回文本字符串

}

程序員必備技能,在java代碼實現替換html標籤,空格,換行符

2 新建replaceWrap方法

這個方法主要是替換傳入參數裡面的空格和換行符標籤

/**

* 替換傳入參數裡面的空格和換行符標籤

* @param strTmp

* @return

*/

public static String replaceWrap(String strTmp) {

String retStr = "";

if (strTmp!=null) {

Pattern p = Pattern.compile("\\s*|\t|\r|\n");

Matcher m = p.matcher(strTmp);

retStr = m.replaceAll("");

}

return retStr;

}

程序員必備技能,在java代碼實現替換html標籤,空格,換行符

3 測試方法

/**

* 測試方法

* @param args

*/

public static void main(String[] args) {

String str = "<p> "+

"親愛的 讀者 您好:<br />"+

"您的預約書已送達取書館。<br />"+

"<br />"+

"預約資料詳細內容:<br />"+

"取書館:<br />"+

"預約書名:《隋唐演義》<br />"+

"館藏條形碼號:ASD004<br />"+

"<br />"+

"預約書保留取書期限至:2017-11-23<br />"+

"請您在期限前取書。<br />"+

"此為系統自動發出,請勿回覆此信,謝謝~~.<br />"+

"<br />"+

"OO立圖書館敬啟</p>";

String str2 = SystemUtils.replaceHTMLTag(str);

System.out.println(SystemUtils.replaceWrap(str2));

}

本文講述了在java代碼裡面是如何實現替換html標籤的實現方式,這也是我們程序員開發必須要掌握的知識,具體代碼:

1 新建replaceHTMLTag方法

這個方法主要是替換傳入參數裡面的html,script,style標籤

/**

* 替換傳入參數裡面的html,script,style標籤

* @param strTmp

* @return

*/

public static String replaceHTMLTag(String strTmp) {

// 腳本的正則表達式

String scriptRegEx = "<script[^>]*?>[\\s\\S]*?<\\/script>";

// 樣式的正則表達式

String styleRegEx = "<style[^>]*?>[\\s\\S]*?<\\/style>";

// 網頁標籤的正則表達式

String htmlRegEx = "<[^>]+>";

Pattern scriptP = Pattern.compile(scriptRegEx, Pattern.CASE_INSENSITIVE);

Matcher scriptM = scriptP.matcher(strTmp);

// 替換script標籤為空白

strTmp = scriptM.replaceAll("");

Pattern styleP = Pattern.compile(styleRegEx, Pattern.CASE_INSENSITIVE);

Matcher styleM = styleP.matcher(strTmp);

// 替換style標籤為空白

strTmp = styleM.replaceAll("");

Pattern htmlP = Pattern.compile(htmlRegEx, Pattern.CASE_INSENSITIVE);

Matcher htmlM = htmlP.matcher(strTmp);

// 替換html標籤為空白

strTmp = htmlM.replaceAll("");

return strTmp.trim(); // 返回文本字符串

}

程序員必備技能,在java代碼實現替換html標籤,空格,換行符

2 新建replaceWrap方法

這個方法主要是替換傳入參數裡面的空格和換行符標籤

/**

* 替換傳入參數裡面的空格和換行符標籤

* @param strTmp

* @return

*/

public static String replaceWrap(String strTmp) {

String retStr = "";

if (strTmp!=null) {

Pattern p = Pattern.compile("\\s*|\t|\r|\n");

Matcher m = p.matcher(strTmp);

retStr = m.replaceAll("");

}

return retStr;

}

程序員必備技能,在java代碼實現替換html標籤,空格,換行符

3 測試方法

/**

* 測試方法

* @param args

*/

public static void main(String[] args) {

String str = "<p> "+

"親愛的 讀者 您好:<br />"+

"您的預約書已送達取書館。<br />"+

"<br />"+

"預約資料詳細內容:<br />"+

"取書館:<br />"+

"預約書名:《隋唐演義》<br />"+

"館藏條形碼號:ASD004<br />"+

"<br />"+

"預約書保留取書期限至:2017-11-23<br />"+

"請您在期限前取書。<br />"+

"此為系統自動發出,請勿回覆此信,謝謝~~.<br />"+

"<br />"+

"OO立圖書館敬啟</p>";

String str2 = SystemUtils.replaceHTMLTag(str);

System.out.println(SystemUtils.replaceWrap(str2));

}

程序員必備技能,在java代碼實現替換html標籤,空格,換行符

請大家多多關注我的頭條號,謝謝大家!

相關推薦

推薦中...