如何删除<script></script>标签之间的文本

5

我想要移除在<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 的结果。有人能帮我确定一下,为什么我的代码会出现错误吗?

4个回答

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());

如果您想删除脚本标记,可以添加以下行:
sb.toString().replace(startTag, "").replace(endTag, "")

更新

如果你不想使用 StringBuilder,你可以这样做:

    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);

1
您可以使用正则表达式来删除脚本标签的内容:
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;

你真的测试过这个吗? - Kagiso Marvin Molekwa
我会将其变成惰性的。 - Pranaya Tomar
对我不起作用。 - Dale

0

我知道我可能晚了。但我想给你一个正则表达式(真的经过测试的解决方案)。

在这里需要注意的是,当涉及到正则表达式时,默认情况下它们的引擎是贪婪的。因此,搜索串如 <script>(.*)</script> 将匹配从 <script> 开始直到行末或文件末尾(取决于使用的 regexp 选项)的整个字符串。这是因为搜索引擎默认使用贪婪匹配。

现在,为了以准确的方式执行所需的匹配...您可以使用“惰性”搜索。

通过惰性加载进行搜索 <script>(.*?)<\/script>

有了这个,您将获得准确的结果。

您可以在 此处 阅读更多关于 Regexp Lazy & Greedy 的内容。


0
这对我有用:
private static String removeScriptTags(String message) {
  String scriptRegex = "<(/)?[ ]*script[^>]*>";
  Pattern pattern2 = Pattern.compile(scriptRegex);

  if(message != null) {
        Matcher matcher2 = pattern2.matcher(message);
        StringBuffer str = new StringBuffer(message.length());
        while(matcher2.find()) {
          matcher2.appendReplacement(str, Matcher.quoteReplacement(" "));
        }
        matcher2.appendTail(str);
        message = str.toString();
  }
 return message;
}

感谢nealvs:https://nealvs.wordpress.com/2010/06/01/removing-tags-from-a-string-in-java/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接