2010. 3. 24. 12:15 COMPUTER/JAVA, JSP
[JAVA/JSP] 정규표현식 스크립트 제거 Regex Replace Function
<script~, onclick=~, onmouseover=~, onmouseout=~ 제거함수

정규표현식 클래스 임포트
import java.util.regex.*;

함수 
public String getRemoveScript(String strContent){
  Pattern patternTag=Pattern.compile("\\<(\\/?)(\\w+)*([^<>]*)>");
  Pattern patternScript=Pattern.compile("(?i)\\<script(.*?)</script>");
  Pattern patternMouseOver=Pattern.compile("(?i) onmouseover=[\"']?([^>\"']+)[\"']*");
  Pattern patternMouseOut=Pattern.compile("(?i) onmouseout=[\"']?([^>\"']+)[\"']*");
  Pattern patternMouseClick=Pattern.compile("(?i) onclick=[\"']?([^>\"']+)[\"']*");
  Matcher matcherContent=patternScript.matcher(strContent);
  strContent=matcherContent.replaceAll("");
  Matcher matcherMouseOver=patternMouseOver.matcher(strContent);
  strContent=matcherMouseOver.replaceAll("");
  Matcher matcherMouseOut=patternMouseOut.matcher(strContent);
  strContent=matcherMouseOut.replaceAll("");
  Matcher matcherMouseClick=patternMouseClick.matcher(strContent);
  strContent=matcherMouseClick.replaceAll("");
  return strContent;
 }

최근에 올라온 글

최근에 달린 댓글