【发布时间】:2026-02-02 06:05:01
【问题描述】:
我有一个这样的html字符串
<div>
<p>
All set to go in.
Finally in.
</p>
<pre>
Yup this text is present
inside of the pre tag.
</pre>
</div>
我想用 br> 标签替换 pre 标签内的换行符。
最终结果应该是。
<div>
<p>
All set to go in.
Finally in.
</p>
<pre>Yup this text is present<br> inside of the pre tag.</pre>
</div>
到目前为止我尝试了什么?
我尝试使用正则表达式并创建了如下所示的模式:-
/is(?=.*<\/pre>)/g 只能找到 </pre> 标记之前的“是”。我还想在其中包含另一个条件,即它应该在<pre> 标记之后。在https://regex101.com/r/qW7tZ1/5 试试这个正则表达式
How do I replace all line breaks in a string with <br /> tags? 不符合我的条件,因为我不想替换所有出现的换行符。
【问题讨论】:
-
您是在浏览器中还是在节点中执行此操作?
-
我在 react-native 工作。
-
<pre>块的内容是一个字符串,那么为什么链接的问题不能提供解决方案? -
@Andreas 链接问题替换字符串中存在的所有断行。但我只想替换那些存在于
标签
标签: javascript html regex react-native