I have text with several line break tags like so
<br></br>
and several double line breaks like so
<br></br><br></br>
I want to get rid of the single <br></br>
, but I want to keep the double ones Using jquery or Jscript how can I tell it to do this When I used .filter("<br></br>")
it did not work because it got rid of both
Is there a way to replace the instances of <br></br><br></br>
with replaceWith? Or some other way
The easiest way is the three-step process:
text = text.
replace(/<br><\/br><br><\/br>/g, '____MAGICMARKER____').
replace(/<br><\/br>/g, '').
replace(/____MAGICMARKER____/g, '<br></br><br></br>')
hoping that ____MAGICMARKER____
is never, nor will ever, be a part of your text. Another possibility (and a better, foolproof one) would be:
text = text.replace(/(<br><\/br>)+/g, function(all, once) {
if (all == once) return "";
return all;
});
Note that this is all written assuming your premise in the question is correct: you have a text (ie a string). If you have a document fragment, then you want to do this operation on its innerHTML
.
Try utilizing html
, RegExp
/<br><br>+[\\s|\\n]/g
to match <br><br>
followed by space character or new line character
$("body").html(function(i, html) { return html.replace(/<br><br>+[\\s|\\n]/g, "") }); console.log($("body").html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <body> <br></br> and several double line breaks like so <br></br><br></br> <br></br> and several double line breaks like so <br></br><br></br> <br></br> and several double line breaks like so <br></br><br></br> </body>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.