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; }