-1

我目前正在使用从 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> 


4

1 回答 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. 正则表达式\\s+用于匹配空格。
于 2020-05-13T15:14:47.233 回答