?
Solved

JavaScript/REGEX: Close all BR Tags

Posted on 2012-04-10
5
Medium Priority
?
425 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 600 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 1400 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
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…
Suggested Courses

766 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