public class RegexString {
/*
* 过滤指定标签
* @param sourceStr
* @param subStr
* @param regexStr
* 指定标签regex (((<a)|(<A))(.*?)((</a>)|(</A>)))过滤所有链接
* @return String
*/
public static String ReplaceTagByRegex(String sourceStr, String subStr,
String regexStr) {
if (sourceStr == null || sourceStr.trim().equals("")) {
return "";
}
StringBuffer substring = new StringBuffer();
Pattern pattern = null;
Matcher matcher = null;
pattern = Pattern.compile(regexStr);
matcher = pattern.matcher(sourceStr);
// 下面的while循环式进行循环匹配替换,把找到的所有
// 符合匹配规则的字串都替换为你想替换的内容
while (matcher.find()) {
matcher.appendReplacement(substring, subStr);
}
matcher.appendTail(substring);
return substring.toString();
}
public static String TrimHtmlTag(String sourceStr) {
return ReplaceTagByRegex(sourceStr,"","<[^>]*>");
}
public static void main(String[] args) {
System.out
.println(TrimHtmlTag("<div><P><a href=''>11111111</a></p><a href=''>ferfefe</a>"));
}
}
js去除标签
function trimHtml(str){
str = str.replace(/(\n)/g, "");
str = str.replace(/(\t)/g, "");
str = str.replace(/(\r)/g, "");
str = str.replace(/<\/?[^>]*>/g, "");
str = str.replace(/\s*/g, "");
str = str.replace(/<[^>]*>/g,"");
return str;
}