正则表达式、JEditorPane、自关闭标签

发布于 2024-08-27 07:56:20 字数 237 浏览 8 评论 0原文

我正在使用 JEditorPane 呈现基本 HTML。但它会错误地呈现自闭合标签,特别是 br 标签,例如
很糟糕,但是
很好。我想使用 String.replaceAll(regex, "
") 来修复 HTML,其中 regex 是一个正则表达式,匹配任何不区分大小写的自闭合 br 标记,并且 " 之间的空格数为零到无穷大r”和“/”(例如,


感谢任何可以解决这个问题的正则表达式专家!

I'm am using JEditorPane to render basic HTML. But it renders self-closing tags incorrectly, specifically br tags, e.g. <br /> is bad but <br> is good. I would like to use String.replaceAll(regex, "<br>") to fix the HTML, where regex is a regular expression matching any self-closing br tag with case-insensitivity and zero to infinity number of spaces between the "r" and the "/" (e.g., <br/>, <BR/>, <br />, <Br     />, etc.).

Thanks to any regular expression experts who can solve this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一向肩并 2024-09-03 07:56:20

您可以使用正则表达式:

<[bB][rR]\s*/>
  • < :匹配文字 <
  • [bB] :匹配的 char 类
    bB
  • [rR] :匹配的 char 类
    rR
  • \s :任何一个空格
  • \s* :零个或多个空格。

如果您只想允许一个空格作为空白,您可以使用:

<[bB][rR] */>

You can use the regex:

<[bB][rR]\s*/>
  • < : To match a literal <
  • [bB] : A char class that matches
    either b or B
  • [rR] : A char class that matches
    either r or R
  • \s : Any one white space
  • \s* : zero or more white spaces.

If you want to allow only a space for a white space you can use:

<[bB][rR] */>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文