我想刪除<script></script>
標籤之間的內容。我正在使用while循環手動檢查模式並使用iterating
。但是,我在這行越來越StringOutOfBoundException
:如何刪除<script>之間的文本</script>標籤
String script=source.substring(startIndex,endIndex-startIndex);
下面是完整的方法:
public static String getHtmlWithoutScript(String source){
String START_PATTERN = "<script>";
String END_PATTERN = " </script>";
while(source.contains(START_PATTERN)){
int startIndex=source.lastIndexOf(START_PATTERN);
int endIndex=source.indexOf(END_PATTERN,startIndex);
String script=source.substring(startIndex,endIndex);
source.replace(script,"");
}
return source;
}
難道我做錯了什麼嗎?我得到endIndex=-1
。任何人都可以幫助我確定,爲什麼我的代碼打破了。
在此先感謝