www.webdeveloper.com
Results 1 to 3 of 3

Thread: Replacing <br> with line breaks

  1. #1
    Join Date
    Sep 2007
    Posts
    275

    Replacing <br> with line breaks

    I have this working, but I would like to take it one step further. Most of my content has two <br /> tags in a row to space out sentances. This is not how I want, but this is how it is. Since I load this content up into a textarea, I need to strip the <br /> tags and replace them with line breaks so the user does not see the break tags, I use:

    Code:
    replace(/<br \/>/g, "\n");
    My problem comes because there are two (<br /><br />) tags right after one another, so the replace function creates two line breaks, which I don't want. Is there a way to check if there are two break tags, and if there are, only replace them with one line break?

  2. #2
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    put the tag inside a parenthesis so you can check one or more times the pattern:
    Code:
    replace(/(<br \/>)+/g, "\n");

  3. #3
    Join Date
    May 2008
    Posts
    381
    Or to make it work for HTML and XHTML, e.g.:
    Code:
    <br>HTML (not XHTML).
    OR
    <br/>No space.
    OR
    <br />Single space.
    OR
    <br            />Multiple spaces.
    OR
    <br	/>Single tab instead of multiple spaces.
    Code:
    replace(/(<br\s*\/?>)+/g, "\n");
    Of course, it replaces any number of those with any combination of whitespace characters before the "/>" or the ">", depending on whether it is <br/> or <br> respectively.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles