
Функция подсчета слов в строке на Javascript
Хочу обратить внимание, что мы будем считать именно слова, а не символы.
И в этом случае нам поможет регулярное выражение.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wordCount(str) { | |
var m = str.match(/[^\s]+/g); | |
return m ? m.length : 0; | |
} |