Manipulating string with html tags using regex

2019-08-30 07:21发布

I have the string test1 test2 tes<span style="color:red;">t3</span> span test4 and I want to put span tag surrounding the span word which position is after </span> without touching the existing span tag using javascript regex.

1条回答
Emotional °昔
2楼-- · 2019-08-30 07:37

REgex:

(<\/span> )(span)

REplacement string:

$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'
查看更多
登录 后发表回答