我想要移除在<script></script>
标签之间的内容。我正在手动检查模式并使用while循环来进行迭代。但是,我在这行代码上遇到了StringOutOfBoundException
错误:
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
的结果。有人能帮我确定一下,为什么我的代码会出现错误吗?