Java代码
代码如下 | 复制代码 |
import java.util.regex.Matcher; import java.util.regex.Pattern;
public class HtmlRegexpUtil { private final static String regxpForHtml = "<([^>]*)>"; // 过滤所有以<开头以>结尾的标签 private final static String regxpForImgTag = "<]*)s">s*imgs+([^>]*)s*>"; // 找出IMG标签 private final static String regxpForImaTagSrcAttrib = "src="([^"]+)""; // 找出IMG标签的SRC属性
public HtmlRegexpUtil() { // TODO Auto-generated constructor stub }
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()); }
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; } |