Javascript正则表达式替换HTML标记 [英] Javascript Regex Replace HTML Tags

查看:78
本文介绍了Javascript正则表达式替换HTML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用正则表达式时遇到很多困难。

Having a lot of difficulties using regex.

继续我想做的事情......

Heres what i am trying to do...

text<div> text </div><div> text </div><div> text </div>

将其转入

text<br> text<br>text<br>text

我试过做...

newhtml = newhtml.replace(/\<div>/g,'<br>');
newhtml = newhtml.replace(/\</div>/g,' ');

但是输出错误。 jquery是否提供了一种更好的方法呢?

but this gives the wrong output. Does jquery provide a better way of doing this?

推荐答案

那是因为你正在逃避错误的事情,因为只有反斜杠需要要转义。

That's because you're escaping the wrong thing, as only the backslash needs to be escaped.

newhtml = newhtml.replace(/<div>/g,'<br>');
newhtml = newhtml.replace(/<\/div>/g,' ');

这篇关于Javascript正则表达式替换HTML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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