使用正则表达式来处理带有html标签的字符串 [英] Manipulating string with html tags using regex

查看:203
本文介绍了使用正则表达式来处理带有html标签的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串 test1 test2 tes< span style =color:red;> t3< / span> span test4 并且我想把span标签放在 span 这个词的后面,这个词的位置在< / span> ,而不使用JavaScript正则表达式触及现有的span标签。

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.

推荐答案

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'

这篇关于使用正则表达式来处理带有html标签的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆