javascript - Regular expression to match multiple consecutive characters and replace them with one character
怪我咯
怪我咯 2017-05-18 11:02:12
0
3
773

Regular expressions are rarely used, so I’m here to ask you for advice. . .

var reg=/<p><br><\/p><p><br><\/p><p><br><\/p>/;
var con=$('#content').html().replace(reg, '<p><br></p>');

That is, I want to match the consecutive /<p>
</p> into one, so as not to see all line breaks. Now, three reg matches are replaced with one p tag. This is problematic. . . . . . For example, if I have five p tags, he will output three p tags. . . Because after matching the first three, he will continue matching from the second p tag! Asking for help

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
淡淡烟草味
var str = '<p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p>'
var reg=/<p>(<br>|<p>|<\/p>)*<br><\/p>/gi;
console.log(str.replace(reg, '<p><br></p>'));
过去多啦不再A梦

var reg = /<p>|</p>/g;
var con = $('#content').html().replace(reg, '');
con = '<p>' + con + '</p>';

过去多啦不再A梦
var reg = /(<p><br><\/p>)(\1)+/g;
var str = '<p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p>';
alert(str.replace(reg,'$1'));
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template