programming-examples/js/Vre1/Count number of words in string.js

11 lines
448 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
function count_words()
{
str1= document.getElementById("InputText").value;
//exclude start and end white-space
str1 = str1.replace(/(^\s*)|(\s*$)/gi,"");
//convert 2 or more spaces to 1
str1 = str1.replace(/[ ]{2,}/gi," ");
// exclude newline with a start spacing
str1 = str1.replace(/\n /,"\n");
document.getElementById("noofwords").value = str1.split(' ').length;
}