When I was developing some time ago, I had to read the introduction content of an article (that is, the first 200 characters). I used hidden fields. Someone might ask, and the background can also intercept characters. That is because the editor contains the html tag, so the background needs to process the regular expression of the html tag. I searched the Internet a few days ago and found that a class that someone wrote was shared with everyone. Don’t be out of date...
import java.util.regex.Matcher; import java.util.regex.Pattern; /** * <p> * Title: HTML-related regular expression tool class* </p> * <p> * Description: Including filtering HTML tags, converting HTML tags, and replacing specific HTML tags* </p> * <p> * Copyright: Copyright (c) 2006 * </p> * * @author hejian * @version 1.0 * @createtime 2006-10-16 */ public class HtmlRegexpUtil { private final static String regxpForHtml = "<([^>]*)>"; // Filter all tags that start with < and end with> private final static String regxpForImgTag = "<//s*img//s+([^>]*)//s*>"; // Find the IMG tag private final static String regxpForImaTagAttrib = "src=/"([^/"]+)/""; // Find the SRC attribute of the IMG tag/** * */ public HtmlRegexpUtil() { // TODO Auto-generated constructor stub } /** * * Basic functions: replace the tag to display normally* <p> * * @param input * @return String */ public String replaceTag(String input) { if (!hasSpecialChars(input)) { return input; } StringBuffer filtered = new StringBuffer(input.length()); char c; for (int i = 0; i <= input.length() - 1; i++) { c = input.charAt(i); switch (c) { case '<': filtered.append("<"); break; case '>': filtered.append(">"); break; case '"': filtered.append(""); break; case '&': filtered.append("&"); break; default: filtered.append(c); } } return (filtered.toString()); } /** * Basic function: determine whether the tag exists* <p> * * @param input * @return boolean */ public boolean hasSpecialChars(String input) { boolean flag = false; if ((input != null) && (input.length() > 0)) { char c; for (int i = 0; i <= input.length() - 1; i++) { c = input.charAt(i); switch (c) { case '>': flag = true; break; case '<': flag = true; break; case '"': flag = true; break; case '&': flag = true; break; } } } return flag; } /** * Basic function: filter all tags starting with "<" and ending with ">"* <p> * * @param str * @return String */ public static String filterHtml(String str) { Pattern pattern = Pattern.compile(regxpForHtml); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); boolean result1 = matcher.find(); while (result1) { matcher.appendReplacement(sb, ""); result1 = matcher.find(); } matcher.appendTail(sb); return sb.toString(); } /** * Basic function: filtering specified tag* <p> * * @param str * @param tag * Specify tag* @return String */ public static String fitHtmlTag(String str, String tag) { String regxp = "<//s*" + tag + "//s+([^>]*)//s*>"; Pattern pattern = Pattern.compile(regxp); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); boolean result1 = matcher.find(); while (result1) { matcher.appendReplacement(sb, ""); result1 = matcher.find(); } matcher.appendTail(sb); return sb.toString(); } /** * Basic function: replace the specified tag* <p> * * @param str * @param beforeTag * Label to replace* @param tagAttrib * Label attribute value to replace* @param startTag * New tag start tag* @param endTag * New tag end tag* @return String * @For example: The src attribute value of the replacement img tag is [img] attribute value[/img] */ public static String replaceHtmlTag(String str, String beforeTag, String tagAttrib, String startTag, String endTag) { String regxpForTag = "<//s*" + beforeTag + "//s+([^>]*)//s*>"; String regxpForTagAttrib = tagAttrib + "=/"([^/"]+)/""; Pattern patternForTag = Pattern.compile(regxpForTag); Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib); Matcher matcherForTag = patternForTag.matcher(str); StringBuffer sb = new StringBuffer(); boolean result = matcherForTag.find(); while (result) { StringBuffer sbreplace = new StringBuffer(); Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag.group(1)); if (matcherForAttrib.find()) { matcherForAttrib.appendReplacement(sbreplace, startTag + matcherForAttrib.group(1) + endTag); } matcherForTag.appendReplacement(sb, sbreplace.toString()); result = matcherForTag.find(); } matcherForTag.appendTail(sb); return sb.toString(); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.