Hello friends, today I will tell you through this tutorial how you can count any string word by javascript. I will tell you step by step through this tutorial.
Count Words Using JavaScript or Jquery, Count Words with JavaScript, Count Words in String Using JavaScript, How to Count Words by JavaScript or Jquery?
<!DOCTYPE html> <html> <head> <title>How to Count Words Using JavaScript?</title> </head> <body> <h1>Count Words Using JavaScript</h1> <form> <textarea name="stringvalue" id="stringvalue" cols="50" rows="4">How to Count Words Using JavaScript?</textarea> <br> <input type="button" name="Convert" value="Count Words" onclick="countstringWords();"> <input name="wordcount" id="wordcount" type="text" value=""> </form> <script> function countstringWords(){ svalue = document.getElementById("stringvalue").value; svalue = svalue.replace(/(^\s*)|(\s*$)/gi,""); svalue = svalue.replace(/[ ]{2,}/gi," "); svalue = svalue.replace(/\n /,"\n"); document.getElementById("wordcount").value = svalue.split(' ').length; } </script> </body> </html>