JavaScript/REGEX: Close all BR Tags

Posted on 2012-04-10
Last Modified: 2012-04-19
Using JavaScript and REGEX, how can I close all BR tags?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">

<script type="text/javascript">
var str = '<br>This is a test <br > and ONLY a test<br class="xyz">.  All BR tags <br /> should be proporly closed. ';

alert( closeBR(str) );

function closeBR(str) {
return str;


Open in new window

Question by:hankknight
  • 3
  • 2
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 150 total points
ID: 37828054

function closeBR(str) {
    return str.replace(/<br *>/g, '<br />');

Open in new window

LVL 42

Accepted Solution

David S. earned 350 total points
ID: 37830811
Here's one way to do it (which will work in very old browsers):
function closeBR(str) {
  return str.replace(/(<br[^>]*)>/gi,'$1 />').replace(/[\s\/]+ \/>/g,' />');

Open in new window

Here's another which uses a single expression and works in browsers in use today:
function closeBR(str) {
  return str.replace(/(<br[^>]*?)\s*\/?>/gi,'$1 />');

Open in new window

@kaufmed  Your code doesn't take into account the case when the element has an attribute.
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 37830822
No it does not, but I can't recall the last time I've seen a line break which had attributes (though I acknowledge it can take them).
LVL 42

Expert Comment

by:David S.
ID: 37830837
I pointed it out because Hank included one such instance in his example. Also he (or someone else who reads this) might want to use the same expression/function to do the same thing for other empty elements, e.g. <img> elements.

Once in a while I see a newbie that gives an attribute to <br> elements.
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 37831979
So he did. I simply overlooked it. Good eye  = D

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Where to begin studying? 5 66
getting id from database 5 24
angularjs controller not found 1 18
angularls and plnkr 14 18
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now