2015-09-29 55 views
2

我想刪除<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。任何人都可以幫助我確定,爲什麼我的代碼打破了。

在此先感謝

回答

3
String text = "<script>This is dummy text to remove </script> dont remove this"; 
    StringBuilder sb = new StringBuilder(text); 
    String startTag = "<script>"; 
    String endTag = "</script>"; 

    //removing the text between script 
    sb.replace(text.indexOf(startTag) + startTag.length(), text.indexOf(endTag), ""); 

    System.out.println(sb.toString()); 

如果你想刪除腳本標籤也增加如下一行:如果你不想使用StringBuilder

sb.toString().replace(startTag, "").replace(endTag, "") 

UPDATE你可以這樣做:

String text = "<script>This is dummy text to remove </script> dont remove this"; 
    String startTag = "<script>"; 
    String endTag = "</script>"; 

    //removing the text between script 
    String textToRemove = text.substring(text.indexOf(startTag) + startTag.length(), text.indexOf(endTag)); 
    text = text.replace(textToRemove, ""); 

    System.out.println(text); 
2

您可以使用正則表達式來刪除腳本標籤內容:

public String removeScriptContent(String html) { 
     if(html != null) { 
      String re = "<script>(.*)</script>"; 

      Pattern pattern = Pattern.compile(re); 
      Matcher matcher = pattern.matcher(html); 
      if (matcher.find()) { 
       return html.replace(matcher.group(1), ""); 
      } 
     } 
     return null; 
    } 

你必須添加此兩名外援:

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
相關問題