Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6974209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:12:28+00:00 2026-05-27T17:12:28+00:00

I need to create a bit of Javascript that can search inputted HTML from

  • 0

I need to create a bit of Javascript that can search inputted HTML from a text box and ignore all the tags to automatically word wrap at a set number like say 70 and add a <br> tag.

I also need to find all the ascii like &copy; and &#150; and count that as one space not 5 or 4 spaces.

So the code would take:

<b>Hello</b> Here is some code that I would like to wrap. Lets pretend this goes on for over 70 spaces.

Output would be:

<b>Hello</b> Here is some code that I would like to wrap. Lets pretend <br>
this goes on for over 70 spaces.

Is this possible? How would I begin? Is there already a tool for this?

By the way CSS is out of the question to use.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:12:28+00:00Added an answer on May 27, 2026 at 5:12 pm

    While the combination of the phrases “regular expression” and “parse HTML” usually causes entire universes to crumble, your use case seems simplistic enough that it could work, but the fact that you want to preserve HTML formatting after wrapping makes it much easier to just work on a space-delimited sequence. Here is a very rough approximation of what you’d like to do:

    input = "<b>Hello</b> Here is some code that I would like to wrap. Let's pretend this goes on for over 70 spaces. Better &yen;&euro;&#177;, let's <em>make</em> it go on for more than 70, and pick &uuml;&thorn; a whole <strong>bu&ntilde;&copy;h</strong> of crazy symbols along the way.";
    words = input.split(' ');
    
    lengths = [];
    for (var i = 0; i < words.length; i++)
      lengths.push(words[i].replace(/<.+>/g, '').replace(/&.+;/g, ' ').length);
    
    line = [], offset = 0, output = [];
    for (var i = 0; i < words.length; i ++) {
      if (offset + (lengths[i] + line.length - 1) < 70) {
        line.push(words[i]);
        offset += lengths[i];
      }
      else {
        output.push(line.join(' '));
        offset = 0; line = [], i -= 1;;
      }
      if (i == words.length - 1)
        output.push(line.join(' '));
    }
    
    output = output.join('<br />');
    

    which results in

    Hello Here is some code that I would like to wrap. Let's pretend this
    goes on for over 70 spaces. Better ¥€±, let's make it go on for more
    than 70, and pick üþ a whole buñ©h of crazy symbols along the way.
    

    Note that the HTML tags (b, em, strong) are preserved, it’s just that Markdown doesn’t show them.

    Basically, the input string is split into words at each space, which is naïve and likely to cause trouble, but it’s a start. Then, the length of each word is calculated after anything resembling an HTML tag or entity has been removed. Then it’s a simple matter of iterating over each word, keeping a running tally of the column we’re on; once we’ve struck 70, we pop the aggregated words into the output string and reset. Again, it’s very rough, but it should suffice for most basic HTML.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I emulate classes (and namespaces) in JavaScript? I need to create a
I need to render a URL for a JavaScript search that I am doing.
I need to create a simple rich-text editor that saves its contents to an
I need to create a historical timeline starting from 1600's to the present day.
I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I need to create a backup of a SQL Server 2005 Database that's only
I have a JavaScript widget (a piece of embedded JS and HTML code) that's
I have some html that creates a dropdown list. The list has text values
I need create custom dialog and put JPanel into it. Is it possible?
i need create an email list sending to many emails. what is best solution

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.