Use JavaScript to count words in a string, WITHOUT using a regex - Stack Overflow

There are many posts on the topic of how to count the number of words in a string in JavaScript already

There are many posts on the topic of how to count the number of words in a string in JavaScript already, and I just wanted to make it clear that I have looked at these.

Counting words in string

Count number of words in string using JavaScript

As a very new programmer I would like to perform this function without the use of any regular expressions. I don't know anything about regex and so I want to use regular code, even if it is not the most effective way in the real world, for the sake of learning.

I cannot find any answer to my question elsewhere, so I thought I would ask here before I default to just using a regex.

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

My overall goal is to find the shortest word in the string.

There are many posts on the topic of how to count the number of words in a string in JavaScript already, and I just wanted to make it clear that I have looked at these.

Counting words in string

Count number of words in string using JavaScript

As a very new programmer I would like to perform this function without the use of any regular expressions. I don't know anything about regex and so I want to use regular code, even if it is not the most effective way in the real world, for the sake of learning.

I cannot find any answer to my question elsewhere, so I thought I would ask here before I default to just using a regex.

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

My overall goal is to find the shortest word in the string.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Nov 2, 2016 at 16:46 Mote ZartMote Zart 9603 gold badges18 silver badges36 bronze badges 6
  • 2 stackoverflow./questions/18679576/… this is your first link, the accepted answer does it without regex. Does that not work? – VLAZ Commented Nov 2, 2016 at 16:49
  • 1 How would you count the words in a string manually? You would put your finger at the start of the string, see if the word you wanted was there, and then move your finger to the next character in the string. Repeat until your finger is at the end of the string. Now do that, but in JS code. – RB. Commented Nov 2, 2016 at 16:50
  • Could just use String.split() to split the string into an array by splitting them by space. Also what's your aversion to Regex? It would be part of the learning curve when learning to program – Harmelodic Commented Nov 2, 2016 at 16:50
  • Seems to me the links provided have sufficient material to answer your question. If not, after reading them, you should realize you need to add what word boundaries are for you. – ASDFGerte Commented Nov 2, 2016 at 16:50
  • My overall goal is to find the shortest word in the string. that's a pletely different question, though, isn't it? – VLAZ Commented Nov 2, 2016 at 16:56
 |  Show 1 more ment

2 Answers 2

Reset to default 1

So, here is the answer to your new question:

My overall goal is to find the shortest word in the string.

It splits the string as before, sorts the array from short to long words and returns the first entry / shortest word:

function WordCount(str) { 
    var words = str.split(" ");

    var sortedWords = words.sort(function(a,b) {
        if(a.length<b.length) return -1;
        else if(a.length>b.length) return 1;
        else return 0;
    });
   return sortedWords[0];
}

console.log(WordCount("hello to the world"));

Seems the question has changed a little but the first link in your post is close. Modified to ignore double spaces:

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

console.log(WordCount("hello      world")); // returns 2

No regexes there - just explode the string into an array on the spaces, remove empty items (double spaces) and count the number of items in the array.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745202097a4616387.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信