HTML 태그 제거하기
HTML 태그 제거하기
1. java Script
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div id="test">
<div>Remove all tag</div>
<span>test</span>
</div>
</body>
<script type="text/javascript">
ele = document.getElementById('test');
// 모든 태그를 제거하는 정규식
oriText = ele.innerHTML;
newText = oriText.replace(/(<([^>]+)>)/ig, "");
alert(newText);
// 원하는 태그만 제거하는 정규식
oriText = ele.innerHTML;
newText = oriText.replace(/<(\/span|span)([^>]*)>/gi,"");
alert(newText);
</script>
</html>
2. Java
public class HTMLUtil {
/**
* 모든 HTML 태그를 제거하고 반환한다.
*
* @param html
* @throws Exception
*/
public static String removeTag(String html) throws Exception {
return html.replaceAll("<(/)?([a-zA-Z]*)(\\s[a-zA-Z]*=[^>]*)?(\\s)*(/)?>", "");
}
public static String removeTag(String html,String tag) throws Exception {
return html.replaceAll("<(/)?("+tag+")(\\s"+tag+"*=[^>]*)?(\\s)*(/)?>", "");
}
public static void main(String[] args) {
String oriText = "<div>Remove Span</div> <span>tag only</span>";
try {
System.out.println("1. " + HTMLUtil.removeTag(oriText));
System.out.println("2. " + HTMLUtil.removeTag(oriText,"span"));
System.out.println("3. " + HTMLUtil.removeTag(oriText,"div"));
} catch (Exception e) {
e.printStackTrace();
}
}
}