Solved

JavaScript/REGEX: Close all BR Tags

Posted on 2012-04-10
5
424 Views
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

<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;
}

</script>
</head>
<body>
</body>
</html>

Open in new window

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 75

Assisted Solution

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

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

Open in new window

0
 
LVL 42

Accepted Solution

by:
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.
0
 
LVL 75

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).
0
 
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.
0
 
LVL 75

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

729 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