Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
使用 Java 怎么可能替换 all "\n"but not ">\n"with "<br/>"?
"\n"
">\n"
"<br/>"
我需要它,因为"<br/>"如果我在纯文本上有新行,我想添加 a,但如果我有 HTML,则不需要。
使用否定的lookbehind。
String str = "\n>\n\n"; str = str.replaceAll("(?<!>)\n", "<br />");
这将匹配\n, 然后回溯一个字符以确保前面的字符不是>.
\n
>