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,' ');