Solved

JavaScript/REGEX: Close all BR Tags

Posted on 2012-04-10
5
418 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
  • 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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)
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…

785 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