替换Regex来删除html标签之间的空白。

问题描述 投票:-1回答:1

我目前正在使用由mustachehandlebars模板构建的HTML。

我们的目标是在handlebars生成文本后,通过删除不必要的空白字符来减小文本的大小,但保持属性值和标签的内容不变。

以下面的内容为例。

</p>                                </td>                            </tr>                            <tr>                                <td>

应该变成:



</a></td></tr><tr><td>


And:


<p align="left"> Untouchable text </p>               </td>            </tr> 


应该变成:。


<p align="left"> Untouchable text </p></td></tr> 


java html regex mustache handlebars.java
1个回答
1
投票

你可以使用 replaceAll(">\\s+<", "><") 如下图所示。

public class Main {
    public static void main(String[] args) {
        String s = "<p align=\"left\"> Untouchable text </p>               </td>            </tr>";
        System.out.println(s.replaceAll(">\\s+<", "><"));
    }
}

输出。

<p align="left"> Untouchable text </p></td></tr>

注意:

  1. 检查 这个 了解更多关于 String::replaceAll.
  2. regex, \\s+用于匹配空间。
© www.soinside.com 2019 - 2024. All rights reserved.