Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JavaScript/REGEX: Close all BR Tags

Posted on 2012-04-10
5
Medium Priority
?
426 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 43

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 43

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

Technology Partners: 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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
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 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…

609 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