Counting words in string
Asked Answered
G

33

144

I was trying to count words in a text in this way:

function WordCount(str) {
  var totalSoFar = 0;
  for (var i = 0; i < WordCount.length; i++)
    if (str(i) === " ") { // if a space is found in str
      totalSoFar = +1; // add 1 to total so far
  }
  totalsoFar += 1; // add 1 to totalsoFar to account for extra space since 1 space = 2 words
}

console.log(WordCount("Random String"));

I think I have got this down pretty well, except I think that the if statement is wrong. The part that checks if str(i) contains a space and adds 1.

Edit:

I found out (thanks to Blender) that I can do this with a lot less code:

function WordCount(str) { 
  return str.split(" ").length;
}

console.log(WordCount("hello world"));
Gam answered 8/9, 2013 at 1:35 Comment(8)
Wouldn't str.split(' ').length be an easier method? jsfiddle.net/j08691/zUuzdUntil
Or str.split(' ') and then count the ones that aren't 0 length strings?Bentlee
string.split(' ').length doesn't work. Spaces are not always word borders! What if there is more than one space between two words? What about ". . ." ?Suziesuzuki
If I want to count the number of words in this sentence which has a number of characters between 2 to 3, how can I do this??Saguache
Does this answer your question? Count number of words in string using JavaScriptBantam
Count Total Amount Of Specific Word In a String JavaScript https://mcmap.net/q/37463/-count-total-amount-of-specific-word-in-a-string-javascriptPetry
The only change I'd recommend to your edited result is returning the console.log from inside the function.Pedagogue
@Until that doesn't work if there is more than one whitespace between a wordHoarhound
V
147

Use square brackets, not parentheses:

str[i] === " "

Or charAt:

str.charAt(i) === " "

You could also do it with .split():

return str.split(' ').length;
Viceregent answered 8/9, 2013 at 1:38 Comment(7)
I think I got what you're saying does my code above in the edited original question look ok?Gam
would your solution work where the words are delimited by anything other than the space character? Say by newlines or tabs?Undertaking
@Viceregent good solution but this can give the wrong result for double spaces omitted in a string..Resale
Count Total Amount Of Specific Word In a String JavaScript https://mcmap.net/q/37463/-count-total-amount-of-specific-word-in-a-string-javascriptPetry
@Resale To handle that I did this str.split(' ').filter((word) => { if (word !== '') return word }) It seems to work okay :)Multi
what about new lineWhipstitch
str = " foo bar "; str = str.trim().replace(/\s\s+/g, ' ').split(' ').length; console.log(str);Laaland
T
156

Try these before reinventing the wheels

from Count number of words in string using JavaScript

function countWords(str) {
  return str.trim().split(/\s+/).length;
}

from http://www.mediacollege.com/internet/javascript/text/count-words.html

function countWords(s){
    s = s.replace(/(^\s*)|(\s*$)/gi,"");//exclude  start and end white-space
    s = s.replace(/[ ]{2,}/gi," ");//2 or more space to 1
    s = s.replace(/\n /,"\n"); // exclude newline with a start spacing
    return s.split(' ').filter(function(str){return str!="";}).length;
    //return s.split(' ').filter(String).length; - this can also be used
}

from Use JavaScript to count words in a string, WITHOUT using a regex - this will be the best approach

function WordCount(str) {
     return str.split(' ')
            .filter(function(n) { return n != '' })
            .length;
}

Notes From Author:

You can adapt this script to count words in whichever way you like. The important part is s.split(' ').length — this counts the spaces. The script attempts to remove all extra spaces (double spaces etc) before counting. If the text contains two words without a space between them, it will count them as one word, e.g. "First sentence .Start of next sentence".

Typography answered 8/9, 2013 at 1:54 Comment(6)
Ive just never seen this syntax: s = s.replace(/(^\s*)|(\s*$)/gi,""); s = s.replace(/[ ]{2,}/gi," "); s = s.replace(/\n /,"\n"); what does each line mean? sorry for being so needyGam
anything? this code is very confusing and that website you literally copied and pasted from it is not helpful at all. Im just confused more than anything I get that its supposed to check for words without spaces our double spaces but how? just a million randomly placed characters really doesnt help...Gam
Thats nice all I was asking is for you to explain your code you wrote. Ive never seen the syntax before and wanted to know what it meant. It's ok I made a separate question and somebody answered my question in depth. Sorry for asking for so much.Gam
str.split(/\s+/).length doesn't really work as-is: trailing white space gets treated as another word.Weslee
Both these codes count one word in an empty string. IMO only the second should be used (@IanWorthington already pointed out why). Additionally, an early return should be added: if (s === "") return 0;Heddle
Note it returns 1 for empty input.Girish
V
147

Use square brackets, not parentheses:

str[i] === " "

Or charAt:

str.charAt(i) === " "

You could also do it with .split():

return str.split(' ').length;
Viceregent answered 8/9, 2013 at 1:38 Comment(7)
I think I got what you're saying does my code above in the edited original question look ok?Gam
would your solution work where the words are delimited by anything other than the space character? Say by newlines or tabs?Undertaking
@Viceregent good solution but this can give the wrong result for double spaces omitted in a string..Resale
Count Total Amount Of Specific Word In a String JavaScript https://mcmap.net/q/37463/-count-total-amount-of-specific-word-in-a-string-javascriptPetry
@Resale To handle that I did this str.split(' ').filter((word) => { if (word !== '') return word }) It seems to work okay :)Multi
what about new lineWhipstitch
str = " foo bar "; str = str.trim().replace(/\s\s+/g, ' ').split(' ').length; console.log(str);Laaland
A
29

One more way to count words in a string. This code counts words that contain only alphanumeric characters and "_", "’", "-", "'" chars.

function countWords(str) {
  var matches = str.match(/[\w\d\’\'-]+/gi);
  return matches ? matches.length : 0;
}
Antiserum answered 15/1, 2014 at 17:41 Comment(3)
You don't need to escape ’' in a regex. Use /[\w\d’'-]+/gi to avoid ESLint no-useless-escape warningsLibrarianship
This will count 'test' as a whole word, while the word there is only test. This creates a problem if you're counting repeated words.Dempster
Note that this does not work for non-alphanumeric characters (languages like Arabic).Monoxide
S
22

After cleaning the string, you can match non-whitespace characters or word-boundaries.

Here are two simple regular expressions to capture words in a string:

  • Sequence of non-white-space characters: /\S+/g
  • Valid characters between word boundaries: /\b[a-z\d]+\b/g

The example below shows how to retrieve the word count from a string, by using these capturing patterns.

/*Redirect console output to HTML.*/document.body.innerHTML='';console.log=function(s){document.body.innerHTML+=s+'\n';};
/*String format.*/String.format||(String.format=function(f){return function(a){return f.replace(/{(\d+)}/g,function(m,n){return"undefined"!=typeof a[n]?a[n]:m})}([].slice.call(arguments,1))});

// ^ IGNORE CODE ABOVE ^
//   =================

// Clean and match sub-strings in a string.
function extractSubstr(str, regexp) {
    return str.replace(/[^\w\s]|_/g, '')
        .replace(/\s+/g, ' ')
        .toLowerCase().match(regexp) || [];
}

// Find words by searching for sequences of non-whitespace characters.
function getWordsByNonWhiteSpace(str) {
    return extractSubstr(str, /\S+/g);
}

// Find words by searching for valid characters between word-boundaries.
function getWordsByWordBoundaries(str) {
    return extractSubstr(str, /\b[a-z\d]+\b/g);
}

// Example of usage.
var edisonQuote = "I have not failed. I've just found 10,000 ways that won't work.";
var words1 = getWordsByNonWhiteSpace(edisonQuote);
var words2 = getWordsByWordBoundaries(edisonQuote);

console.log(String.format('"{0}" - Thomas Edison\n\nWord count via:\n', edisonQuote));
console.log(String.format(' - non-white-space: ({0}) [{1}]', words1.length, words1.join(', ')));
console.log(String.format(' - word-boundaries: ({0}) [{1}]', words2.length, words2.join(', ')));
body { font-family: monospace; white-space: pre; font-size: 11px; }

Finding Unique Words

You could also create a mapping of words to get unique counts.

function cleanString(str) {
    return str.replace(/[^\w\s]|_/g, '')
        .replace(/\s+/g, ' ')
        .toLowerCase();
}

function extractSubstr(str, regexp) {
    return cleanString(str).match(regexp) || [];
}

function getWordsByNonWhiteSpace(str) {
    return extractSubstr(str, /\S+/g);
}

function getWordsByWordBoundaries(str) {
    return extractSubstr(str, /\b[a-z\d]+\b/g);
}

function wordMap(str) {
    return getWordsByWordBoundaries(str).reduce(function(map, word) {
        map[word] = (map[word] || 0) + 1;
        return map;
    }, {});
}

function mapToTuples(map) {
    return Object.keys(map).map(function(key) {
        return [ key, map[key] ];
    });
}

function mapToSortedTuples(map, sortFn, sortOrder) {
    return mapToTuples(map).sort(function(a, b) {
        return sortFn.call(undefined, a, b, sortOrder);
    });
}

function countWords(str) {
    return getWordsByWordBoundaries(str).length;
}

function wordFrequency(str) {
    return mapToSortedTuples(wordMap(str), function(a, b, order) {
        if (b[1] > a[1]) {
            return order[1] * -1;
        } else if (a[1] > b[1]) {
            return order[1] * 1;
        } else {
            return order[0] * (a[0] < b[0] ? -1 : (a[0] > b[0] ? 1 : 0));
        }
    }, [1, -1]);
}

function printTuples(tuples) {
    return tuples.map(function(tuple) {
        return padStr(tuple[0], ' ', 12, 1) + ' -> ' + tuple[1];
    }).join('\n');
}

function padStr(str, ch, width, dir) { 
    return (width <= str.length ? str : padStr(dir < 0 ? ch + str : str + ch, ch, width, dir)).substr(0, width);
}

function toTable(data, headers) {
    return $('<table>').append($('<thead>').append($('<tr>').append(headers.map(function(header) {
        return $('<th>').html(header);
    })))).append($('<tbody>').append(data.map(function(row) {
        return $('<tr>').append(row.map(function(cell) {
            return $('<td>').html(cell);
        }));
    })));
}

function addRowsBefore(table, data) {
    table.find('tbody').prepend(data.map(function(row) {
        return $('<tr>').append(row.map(function(cell) {
            return $('<td>').html(cell);
        }));
    }));
    return table;
}

$(function() {
    $('#countWordsBtn').on('click', function(e) {
        var str = $('#wordsTxtAra').val();
        var wordFreq = wordFrequency(str);
        var wordCount = countWords(str);
        var uniqueWords = wordFreq.length;
        var summaryData = [
            [ 'TOTAL', wordCount ],
            [ 'UNIQUE', uniqueWords ]
        ];
        var table = toTable(wordFreq, ['Word', 'Frequency']);
        addRowsBefore(table, summaryData);
        $('#wordFreq').html(table);
    });
});
table {
    border-collapse: collapse;
    table-layout: fixed;
    width: 200px;
    font-family: monospace;
}
thead {
    border-bottom: #000 3px double;;
}
table, td, th {
    border: #000 1px solid;
}
td, th {
    padding: 2px;
    width: 100px;
    overflow: hidden;
}

textarea, input[type="button"], table {
    margin: 4px;
    padding: 2px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<h1>Word Frequency</h1>
<textarea id="wordsTxtAra" cols="60" rows="8">Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.

But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.</textarea><br />
<input type="button" id="countWordsBtn" value="Count Words" />
<div id="wordFreq"></div>
Signal answered 19/5, 2015 at 20:51 Comment(0)
R
18

I think this method is more than you want

var getWordCount = function(v){
    var matches = v.match(/\S+/g) ;
    return matches?matches.length:0;
}
Roryros answered 8/9, 2013 at 1:46 Comment(0)
E
9

String.prototype.match returns an array, we can then check the length,

I find this method to be most descriptive

var str = 'one two three four five';

str.match(/\w+/g).length;
Empyreal answered 24/8, 2016 at 13:54 Comment(1)
potential place to occur an error if string is emptyBurrow
D
6

The easiest way I've find so far is to use a regex with split.

var calculate = function() {
  var string = document.getElementById('input').value;
  var length = string.split(/[^\s]+/).length - 1;
  document.getElementById('count').innerHTML = length;
};
<textarea id="input">My super text that does 7 words.</textarea>
<button onclick="calculate()">Calculate</button>
<span id="count">7</span> words
Decencies answered 31/3, 2016 at 14:33 Comment(0)
J
6

This will handle all of the cases and is as efficient as possible. (You don't want split(' ') unless you know beforehand that there are no spaces of greater length than one.):

var quote = `Of all the talents bestowed upon men, 
              none is so precious as the gift of oratory. 
              He who enjoys it wields a power more durable than that of a great king. 
              He is an independent force in the world. 
              Abandoned by his party, betrayed by his friends, stripped of his offices, 
              whoever can command this power is still formidable.`;

function wordCount(text = '') {
  return text.split(/\S+/).length - 1;
};

console.log(wordCount(quote));//59
console.log(wordCount('f'));//1
console.log(wordCount('  f '));//1
console.log(wordCount('   '));//0
Jeep answered 16/11, 2019 at 23:51 Comment(1)
Count Total Amount Of Specific Word In a String JavaScript https://mcmap.net/q/37463/-count-total-amount-of-specific-word-in-a-string-javascriptPetry
H
6

This one-liner is pretty simple and counts words accurately even if there is more than one whitespace between them:

return string.split(/\s+/).length;

Regex Explanation

Part of Expression Explanation
\s Matches any whitespace character
+ Matches the previous token between one and unlimited times
Hoarhound answered 12/1, 2022 at 0:46 Comment(1)
This code count this "hello " (with space) - as two words. "hello ".split(/\s+/).length - return 2Step
Z
5

The answer given by @7-isnotbad is extremely close, but doesn't count single-word lines. Here's the fix, which seems to account for every possible combination of words, spaces and newlines.

function countWords(s){
    s = s.replace(/\n/g,' '); // newlines to space
    s = s.replace(/(^\s*)|(\s*$)/gi,''); // remove spaces from start + end
    s = s.replace(/[ ]{2,}/gi,' '); // 2 or more spaces to 1
    return s.split(' ').length; 
}
Zaslow answered 27/2, 2017 at 7:27 Comment(0)
M
4
function countWords(str) {
    var regEx = /([^\u0000-\u007F]|\w)+/g;  
    return str.match(regEx).length;
}

Explanation:

/([^\u0000-\u007F]|\w) matches word characters - which is great -> regex does the heavy lifting for us. (This pattern is based on the following SO answer: https://mcmap.net/q/24959/-regular-expression-to-match-non-ascii-characters by @Landeeyo)

+ matches the whole string of the previously specified word characters - so we basically group word characters.

/g means it keeps looking till the end.

str.match(regEx) returns an array of the found words - so we count its length.

Mackinnon answered 21/11, 2017 at 9:59 Comment(3)
I am getting this error: error Unexpected control character(s) in regular expression: \x00 no-control-regexDysphemia
This regex will trow error if string starts with / or (Harkey
@WalterMonecke just tested it on chrome - didn't get the error. Where did you get an error with this? ThanksMackinnon
D
4
let leng = yourString.split(' ').filter(a => a.trim().length > 0).length
Decencies answered 8/12, 2017 at 9:12 Comment(1)
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Countryside
G
4

For those who want to use Lodash can use the _.words function:

var str = "Random String";
var wordCount = _.size(_.words(str));
console.log(wordCount);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
Gilbert answered 7/11, 2018 at 8:39 Comment(0)
Q
4

Here's my approach, which simply splits a string by spaces, then for loops the array and increases the count if the array[i] matches a given regex pattern.

    function wordCount(str) {
        var stringArray = str.split(' ');
        var count = 0;
        for (var i = 0; i < stringArray.length; i++) {
            var word = stringArray[i];
            if (/[A-Za-z]/.test(word)) {
                count++
            }
        }
        return count
    }

Invoked like so:

var str = "testing strings here's a string --..  ? // ... random characters ,,, end of string";
wordCount(str)

(added extra characters & spaces to show accuracy of function)

The str above returns 10, which is correct!

Quantity answered 12/3, 2019 at 2:49 Comment(1)
Some languages don't use [A-Za-z] at allLipp
U
4

Accuracy is also important.

What option 3 does is basically replace all the but any whitespaces with a +1 and then evaluates this to count up the 1's giving you the word count.

It's the most accurate and fastest method of the four that I've done here.

Please note it is slower than return str.split(" ").length; but it's accurate when compared to Microsoft Word.

See file ops/s and returned word count below.

Here's a link to run this bench test. https://jsbench.me/ztk2t3q3w5/1

// This is the fastest at 111,037 ops/s ±2.86% fastest
var str = "All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.";
function WordCount(str) {
  return str.split(" ").length;
}
console.log(WordCount(str));
// Returns 241 words. Not the same as Microsoft Word count, of by one.

// This is the 2nd fastest at 46,835 ops/s ±1.76% 57.82% slower
var str = "All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.";
function WordCount(str) {
  return str.split(/(?!\W)\S+/).length;
}
console.log(WordCount(str));
// Returns 241 words. Not the same as Microsoft Word count, of by one.

// This is the 3rd fastest at 37,121 ops/s ±1.18% 66.57% slower
var str = "All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.";
function countWords(str) {
  var str = str.replace(/\S+/g,"\+1");
  return eval(str);
}
console.log(countWords(str));
// Returns 240 words. Same as Microsoft Word count.

// This is the slowest at 89 ops/s 17,270 ops/s ±2.29% 84.45% slower
var str = "All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.";
function countWords(str) {
  var str = str.replace(/(?!\W)\S+/g,"1").replace(/\s*/g,"");
  return str.lastIndexOf("");
}
console.log(countWords(str));
// Returns 240 words. Same as Microsoft Word count.
Uruguay answered 10/11, 2019 at 14:42 Comment(0)
S
3

There may be a more efficient way to do this, but this is what has worked for me.

function countWords(passedString){
  passedString = passedString.replace(/(^\s*)|(\s*$)/gi, '');
  passedString = passedString.replace(/\s\s+/g, ' '); 
  passedString = passedString.replace(/,/g, ' ');  
  passedString = passedString.replace(/;/g, ' ');
  passedString = passedString.replace(/\//g, ' ');  
  passedString = passedString.replace(/\\/g, ' ');  
  passedString = passedString.replace(/{/g, ' ');
  passedString = passedString.replace(/}/g, ' ');
  passedString = passedString.replace(/\n/g, ' ');  
  passedString = passedString.replace(/\./g, ' '); 
  passedString = passedString.replace(/[\{\}]/g, ' ');
  passedString = passedString.replace(/[\(\)]/g, ' ');
  passedString = passedString.replace(/[[\]]/g, ' ');
  passedString = passedString.replace(/[ ]{2,}/gi, ' ');
  var countWordsBySpaces = passedString.split(' ').length; 
  return countWordsBySpaces;

}

its able to recognise all of the following as separate words:

abc,abc = 2 words,
abc/abc/abc = 3 words (works with forward and backward slashes),
abc.abc = 2 words,
abc[abc]abc = 3 words,
abc;abc = 2 words,

(some other suggestions I've tried count each example above as only 1 x word) it also:

  • ignores all leading and trailing white spaces

  • counts a single-letter followed by a new line, as a word - which I've found some of the suggestions given on this page don't count, for example:
    a
    a
    a
    a
    a
    sometimes gets counted as 0 x words, and other functions only count it as 1 x word, instead of 5 x words)

if anyone has any ideas on how to improve it, or cleaner / more efficient - then please add you 2 cents! Hope This Helps Someone out.

Soporific answered 17/4, 2017 at 20:41 Comment(0)
D
2

Here's a function that counts number of words in an HTML code:

$(this).val()
    .replace(/((&nbsp;)|(<[^>]*>))+/g, '') // remove html spaces and tags
    .replace(/\s+/g, ' ') // merge multiple spaces into one
    .trim() // trim ending and beginning spaces (yes, this is needed)
    .match(/\s/g) // find all spaces by regex
    .length // get amount of matches
Defalcate answered 19/5, 2017 at 9:13 Comment(0)
P
2

I'm not sure if this has been said previously, or if it's what is needed here, but couldn't you make the string an array and then find the length?

let randomString = "Random String";

let stringWords = randomString.split(' ');
console.log(stringWords.length);
Porch answered 9/8, 2018 at 14:2 Comment(0)
M
2

I think this answer will give all the solutions for:

  1. Number of characters in a given string
  2. Number of words in a given string
  3. Number of lines in a given string

 function NumberOf() { 
		 var string = "Write a piece of code in any language of your choice that computes the total number of characters, words and lines in a given text. \n This is second line. \n This is third line.";

		 var length = string.length; //No of characters
		 var words = string.match(/\w+/g).length; //No of words
		 var lines = string.split(/\r\n|\r|\n/).length; // No of lines

		 console.log('Number of characters:',length);
		 console.log('Number of words:',words);
		 console.log('Number of lines:',lines);


}

NumberOf();
  1. First you need to find length of the given string by string.length
  2. Then you can find number of words by matching them with string string.match(/\w+/g).length
  3. Finally you can split each line like this string.length(/\r\n|\r|\n/).length

I hope this can help those who are searching for these 3 answers.

Marniemaro answered 28/2, 2019 at 8:23 Comment(1)
Excellent. Please change the variable name string to something else. It's confusing. Caused me to think for a second string.match() is a static method. Cheers.Lane
H
2
var str =   "Lorem ipsum dolor sit amet consectetur adipisicing elit. Labore illum fuga magni exercitationem porro? Eaque tenetur tempora nesciunt laborum deleniti, quidem nemo consequuntur voluptate alias ad soluta, molestiae, voluptas libero!" ;

let count = (str.match(/\s/g) || []).length;
console.log(count + 1 );

countWords =(str )=>{

    let count =  ( str.match(/\s/g)   || []  ).length;
    count = (count == 0 ) ? 0 : count +1  ; 
    return count 
}
Hypertension answered 9/5, 2021 at 19:34 Comment(0)
K
2
function countWords(str) {
    str = str.replace(/(^\s*)|(\s*$)/gi,"");
    str = str.replace(/[ ]{2,}/gi," ");
    str = str.replace(/\n /,"\n");
    return str.split(' ').length;
}
document.write(countWords("  this function remove extra space and count the real   string lenth"));
Kenleigh answered 29/6, 2021 at 19:58 Comment(0)
B
2

This snippet will compute how many words are in the sentence:

let para = "hello world I am javascript";
console.log(para.split(" ").filter((x) => x !== "").length)
Brettbretz answered 13/9, 2021 at 9:5 Comment(0)
S
1

You got some mistakes in your code.

function WordCount(str) {
    var totalSoFar = 0;
    for (var i = 0; i < str.length; i++) {
        if (str[i] === " ") {
            totalSoFar += 1;
        }
    }
    return totalSoFar + 1; // you need to return something.
}
console.log(WordCount("Random String"));

There is another easy way using regular expressions:

(text.split(/\b/).length - 1) / 2

The exact value can differ about 1 word, but it also counts word borders without space, for example "word-word.word". And it doesn't count words that don't contain letters or numbers.

Suziesuzuki answered 26/6, 2015 at 14:37 Comment(0)
N
1

I know its late but this regex should solve your problem. This will match and return the number of words in your string. Rather then the one you marked as a solution, which would count space-space-word as 2 words even though its really just 1 word.

function countWords(str) {
    var matches = str.match(/\S+/g);
    return matches ? matches.length : 0;
}
Narcotism answered 14/3, 2016 at 11:9 Comment(0)
K
1
function WordCount(str) {
    var totalSoFar = 0;
    for (var i = 1; i < str.length; i++) {
        if (str[i] === " ") {
            totalSoFar ++;
        }
    }
    return totalSoFar; 
}
console.log(WordCount("hi my name is raj));
Karlynkarma answered 20/11, 2019 at 10:8 Comment(1)
Code-only answers are generally frowned upon on this site. Could you please edit your answer to include some comments or explanation of your code? Explanations should answer questions like: What does it do? How does it do it? Where does it go? How does it solve OP's problem? See: How to anwser. Thanks!Dromedary
M
1

If you want to count specific words

function countWholeWords(text, keyword) {
    const times = text.match(new RegExp(`\\b${keyword}\\b`, 'gi'));

    if (times) {
        console.log(`${keyword} occurs ${times.length} times`);
    } else {
        console.log(keyword + " does not occurs")
    }
}


const text = `
In a professional context it often happens that private or corporate clients corder a publication to be 
made and presented with the actual content still not being ready. Think of a news blog that's 
filled with content hourly on the day of going live. However, reviewers tend to be distracted 
by comprehensible content, say, a random text copied from a newspaper or the internet.
`

const wordsYouAreLookingFor = ["random", "cat", "content", "reviewers", "dog", "with"]

wordsYouAreLookingFor.forEach((keyword) => countWholeWords(text, keyword));


// random occurs 1 times
// cat does not occurs
// content occurs 3 times
// reviewers occurs 1 times
// dog does not occurs
// with occurs 2 times
Mcadams answered 10/1, 2021 at 15:14 Comment(0)
F
1

You can use this algorithm :

app.js :

const TextArea = document.querySelector('textarea');

const CountContainer = document.querySelector('#demo');


TextArea.addEventListener('keypress', () => {
    let TextValue = TextArea.value.split(' ').join('-').split('\n').join('-').split('-');

    let WordCountArray = TextValue.filter(el => {
        return el != '';
    });

    let WordSen = WordCountArray.length <= 1 ? 'Word' : 'Words';

    console.log(WordCountArray);

    CountContainer.textContent = WordCountArray.length + ' ' + WordSen;

});

TextArea.addEventListener('keyup', function () {
    if (this.value === '') CountContainer.textContent = '0 Word';
});

HTML index page for test:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <textarea cols="30" rows="10"></textarea>
    <div id="demo"></div>

    <script src="app.js"></script>
</body>


</html>

Finfoot answered 19/3, 2021 at 20:59 Comment(0)
N
1

Adapted from internals-in answer It handles the edge case ' ' as well

export const countWords = (str: string) => {

  str = str.trim();
  if (!str.length) {
    return str.length
  }
  return str.trim().split(/\s+/).length;
}

Jest tests

    test("countwords", () => {
        expect(countWords('  ')).toBe(0)
        expect(countWords('78   7 ')).toBe(2)
        expect(countWords('78 7 ')).toBe(2)
        expect(countWords('aa, , 7')).toBe(3)
        expect(countWords('aa, , \n \n \t 7 \n 4')).toBe(4)
    }); 
Nunhood answered 8/4, 2021 at 12:2 Comment(0)
M
1

The issue with the standard /s+/ method is that it doesn't deal with hyphenated words. I tried to do it as a single regex (/\s+|\w-\w/ and a few variations) but that failed numerous tests, particularly due to overlapping matches. Eventually I settled on the following:

function wordcount(s) {
    return s.replace(/-/g, ' ').trim().split(/\s+/g).length;
}

It passes the following tests:

const test_sentences = [
    { sentence: "hello world.", word_count: 2 },
    { sentence: "hello world - how are you", word_count: 5 },
    { sentence: "hello 10 world - how are you?", word_count: 6 },
    { sentence: "   hello 10 world - how are you? 10   ", word_count: 7 },
    { sentence: "Test of-hyphens in a-string", word_count: 6 },
    { sentence: "Test of-hyphens -- in an-string ---", word_count: 6 },
    { sentence: "Test-of-hyphens -- in-a-string ---", word_count: 6 },
]
for (let s of test_sentences) {
    console.assert(wordcount(s.sentence) === s.word_count, `Expected ${s.word_count} but got ${wordcount(s.sentence)} for "${s.sentence}"`);
}
Mix answered 22/7, 2022 at 7:54 Comment(1)
right, but a hyphenated word is a single wordVexed
A
0
<textarea name="myMessage" onkeyup="wordcount(this.value)"></textarea>
<script type="text/javascript">
var cnt;
function wordcount(count) {
var words = count.split(/\s/);
cnt = words.length;
var ele = document.getElementById('w_count');
ele.value = cnt;
}
document.write("<input type=text id=w_count size=4 readonly>");
</script>
Anthesis answered 9/1, 2014 at 10:14 Comment(0)
S
0
function totalWordCount() {
  var str ="My life is happy"
  var totalSoFar = 0;

  for (var i = 0; i < str.length; i++)
    if (str[i] === " ") { 
     totalSoFar = totalSoFar+1;
  }
  totalSoFar = totalSoFar+ 1; 
  return totalSoFar
}

console.log(totalWordCount());
Stroud answered 16/3, 2018 at 10:43 Comment(2)
Please add somes explanations editing your answer, avoid code only answerAleppo
What happens if there are multiple consecutive whitespaces?Cutright
M
0

Demo

function countWords(str) {
        // Remove leading and trailing white spaces
        str = str.trim();
        // Split the string by one or more white spaces
        let words = str.split(/\s+/);
        // Return the number of words
        return words.length;
    }

usage:

let sentence = "This is a    sample sentence.";
console.log("Number of words:", countWords(sentence)); 
Muumuu answered 19/2 at 17:56 Comment(0)
M
-1

You can use this simple way:

function wordCounter(text) {
  let arr = text.split('');
  return arr.length;
}
Mathew answered 6/1, 2022 at 1:22 Comment(1)
This is a character counterVexed

© 2022 - 2024 — McMap. All rights reserved.