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 7977721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:17:10+00:00 2026-06-04T09:17:10+00:00

I got this code for counting the number of words from an html editor.

  • 0

I got this code for counting the number of words from an html editor.

(providing htmlData has already been set)
var rawWords = htmlData.replace(/<(?:.|\s)*?>/g, '')
                       .replace(/(\r\n|\n|\r)/gm,' ');
var filteredWords = rawWords.replace(/\[([^\]]+)\]/g,'')
                            .replace(/\s+/g, " ")
                            .replace(/^\s+|\s+$/g, "");

From what I understand, the first line removes the html and then removes any returns.

The next line removes anything in brackets (this is to add notes without affecting the word count) and then removes extra spaces

But if I type this:

Apple


Charlie

Tom

It gives me a word count of 6, not 3. Any idea why? I’m not good at regex!!!!

thanks so much

  • 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-06-04T09:17:12+00:00Added an answer on June 4, 2026 at 9:17 am

    Try this, it’s simple, just splits the whitespace/numbers, and counts the array.

    window.onload = function() {
    
        // get string as text
        var text = document.body.innerText;
    
        // replace all non letters (so we don't count 1 as a word)
        text     = text.replace(/[^a-zA-Z\s]/g, '');
    
        // split on whitespace
        var words = text.split(/[\s]+/);
    
        // output -- 52
        console.log('numwords', words, words.length); // numwords 52
    }
    

    full example below:

    <html>
    <head>
    <script type="text/javascript">// script</script>
    </head>
    <body>
    
    a b c d e f g
    1 1 1 1 1 1 1
    
    
    
    
    the quick brown fox jumped over the lazy dog.
    the quick brown fox jumped over the lazy dog.
    the quick brown fox jumped over the lazy dog.<br><br><br><br><br>
    the quick brown fox jumped over the lazy dog.
    the quick brown fox jumped over the lazy dog.
    
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got this code for a viewscroller from the apple developers site. @synthesize scrollView1, scrollView2;
I got this code from the wordpress <head profile=http://gmpg.org/xfn/11> What does this means? what
I got this code from someone, it's almost perfect to create a dynamic breadcrumb,
I got this code from someone and it works very well, I just want
got this code from a website that helped me in creating buttons and stuff.
Got this code from here http://www.irunmywebsite.com/raphael/drawtool2.php . Runs great in IE when I tested
I got this code below from a tutorial I'm using to learn PHP. I
I got this code from this site: http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx But I'm just a beginner so
I got this code from somewhere, a book I think: public class KITSMSReceiver extends
I got this code from Andy Tanenbaum's book. I am trying to run it.

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.