How to count words in JavaScript using JQuery

前端 未结 7 1200
旧巷少年郎
旧巷少年郎 2020-12-09 13:17

I have a simple html text box. When I \"submit\" the form that the text box is in, I would like to get a variable with the number of words inside using Jqu

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 13:58

    Using the regular expression below allow us to remove any kind of white space (single or multiples) so that the count is very accurate.

    $('#name').keyup(function(){
       var wordCount = $(this).val().split(/[\s\.\?]+/).length;
       console.log(wordCount);
    });
    

    See this jQuery plugin I have developed:

    https://github.com/mcdesignpro/maxLenght

提交回复
热议问题