【问题标题】:Regex replacement to remove whitespace between html tags正则表达式替换以删除 html 标记之间的空格
【发布时间】:2020-08-29 20:23:56
【问题描述】:

我目前正在使用基于 mustache/handlebars 模板构建的 HTML。

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

以以下为例:

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

应该变成:



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


还有:


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


应该变成:


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


【问题讨论】:

    标签: java html regex mustache handlebars.java


    【解决方案1】:

    你可以使用replaceAll("&gt;\\s+&lt;", "&gt;&lt;"),如下图:

    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. 查看this,了解更多关于String::replaceAll的信息。
    2. 正则表达式\\s+用于匹配空格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-28
      • 2014-01-29
      • 2017-04-13
      • 2020-03-11
      • 1970-01-01
      相关资源
      最近更新 更多