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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…