gpt4 book ai didi

javascript - 使用正则表达式操作带 html 标签的字符串

转载 作者:行者123 更新时间:2023-12-03 12:09:10 25 4
gpt4 key购买 nike

我有字符串test1 test2 tes<span style="color:red;">t3</span> span test4我想将 span 标签放在 span 周围</span> 之后的位置的单词无需使用 javascript 正则表达式触及现有的 span 标记。

最佳答案

正则表达式:

(<\/span> )(span)

替换字符串:

$1<span>$2</span>

DEMO

> 'test1 test2 tes<span style="color:red;">t3</span> span test4'.replace(/(<\/span> )(span)/g, "$1<span>$2</span>")
'test1 test2 tes<span style="color:red;">t3</span> <span>span</span> test4'

关于javascript - 使用正则表达式操作带 html 标签的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25042498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com