October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
HTML

How to Replace `
` with `

` in JavaScript

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For the SitePoint example, this jQuery replacement turns each literal <br>* sequence into a paragraph boundary:

$('body').html($('body').html().replace(/<br>*/g, '</p><p>'));

It is a narrow workaround for that exact legacy pattern. The expression removes neither a space after the asterisk nor alternate forms such as <br />, uppercase tags, or whitespace between the tag and asterisk.

What the replacement actually matches

The regular expression /<br>*/g searches for two literal characters in sequence: the exact lowercase tag <br>, followed immediately by *. The g flag applies the replacement to every match in the string.

For example, content shaped like this:

<p>First fragment<br>* Second fragment<br>* Third fragment</p>

becomes:

<p>First fragment</p><p> Second fragment</p><p> Third fragment</p>

Because the replacement is only </p><p>, the space after each asterisk remains. The result may therefore start a new paragraph with a leading space.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Removing the asterisk and its following whitespace

If the input consistently contains an asterisk followed by optional whitespace, include that whitespace in the match:

const converted = html.replace(/<br>*s*/g, '</p><p>');

Here, s* consumes zero or more whitespace characters after the asterisk. Use this only when that whitespace is part of the legacy marker and should always be discarded.

If you need a source line break for readability, use a newline in the replacement, but remember that HTML rendering is governed by markup and CSS:

const converted = html.replace(/<br>*s*/g, '</p>n<p>');

Adapt the match to the real input

Inspect representative database records before choosing a pattern. The original snippet does not match these common variations:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • <br />* or <br/>*
  • <BR>*
  • <br> *, with a space between the tag and asterisk
  • HTML entities or other markers instead of a literal asterisk

For a known, controlled format, a more tolerant expression could be written, for example:

const converted = html.replace(/<brs*/??>s**s*/gi, '</p><p>');

Test any broader pattern against content that includes intentional asterisks, blank lines, existing paragraph tags, and the final fragment. A replacement that is too broad can remove text that was not a separator.

Why server-side conversion is usually preferable

The forum discussion’s preferred long-term fix was to transform the legacy value after reading it from the database and before sending the response to the browser.

  • Visitors who disable JavaScript still receive the converted paragraphs.
  • Paragraphs and line breaks can have different CSS, margins, and layout behavior, so the transformation can be reviewed as part of normal rendering.
  • The database value can remain unchanged while the application consistently emits the desired HTML.

The browser-side approach changes the DOM after delivery; it does not rewrite the original response source stored on the server.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choosing an implementation

Approach Best fit Important limitation
Server-side fixed-string replacement Legacy records follow one known marker format Requires an application change and careful output validation
Client-side jQuery replacement Temporary mitigation when the page already contains the rendered HTML Requires JavaScript and may alter layout only after page load
HTML parser plus targeted transformation Markup varies or may contain nested, malformed, or unrelated HTML More work than replacing one fixed string; choose a parser appropriate to your server language

A regular-expression replacement is narrower than parsing HTML. A related community answer recommends an HTML parser for general HTML parsing, while allowing a regex for a fixed, known template. Do not use the short expression as a general-purpose HTML parser.

Safer browser-side handling

The original workaround rewrites the entire body’s HTML:

$('body').html($('body').html().replace(/<br>*/g, '</p><p>'));

If the legacy text is in one known element, limit the operation to that element instead of replacing unrelated page markup:

const element = document.querySelector('.legacy-content');
if (element) {
  element.innerHTML = element.innerHTML.replace(/<br>*s*/g, '</p><p>');
}

This still assumes the content is trusted and that the pattern is known. Restricting the target reduces accidental changes elsewhere, but it does not turn regex into an HTML parser.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Validation checklist

  • Confirm whether the source uses <br>, <br/>, or <br />.
  • Check capitalization and whitespace around the asterisk.
  • Decide whether the following space should be preserved or consumed.
  • Verify that the input already has a surrounding paragraph structure that will remain valid after inserting closing and opening tags.
  • Check the first and last fragments so you do not create an empty paragraph unintentionally.
  • Compare the rendered result with CSS rules for both br and p.
  • Prefer server-side conversion when the output must work without JavaScript.

The Bottom Line

Use replace(/<br>*/g, '</p><p>') only for the exact legacy sequence it was designed to match. Add whitespace handling when required, adapt the pattern to the actual markup, and move the conversion server-side for a durable result. Use an HTML parser when the input is not a fixed, controlled template.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read next

Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.