如何使用 JQuery 在 JavaScript 中计算单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8752853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:21:34  来源:igfitidea点击:

How to count words in JavaScript using JQuery

javascriptjqueryvalidationword-count

提问by kirby

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 wordsinside using Jquery. I would also like to check if the inputted text is only letters, numbers and hyphens (also in jquery). I do not need to count the words as the user types, just when the form is submitted. The form won't submit if jquery is turned off so I guess there are no security risks by not using php. Is this true?

我有一个简单的 html 文本框。当我“提交”文本框所在的表单时,我想使用 Jquery获取一个包含单词数的变量。我还想检查输入的文本是否只是字母、数字和连字符(也在 jquery 中)。我不需要在用户键入时计算单词,只在提交表单时计算。如果关闭 jquery,表单将不会提交,所以我猜不使用 php 没有安全风险。这是真的?

HTML:

HTML:

<input type='text' name='name' id='name' />
<input type='button' value='Sign Up' id='signUp'>

JQUERY (attempt):

JQUERY(尝试):

var wordcount = $('#name').val()  // i don't know how to count the words 

回答by mrtsherman

You would split the string and then count the length of the resulting array.

您将拆分字符串,然后计算结果数组的长度。

$('input[type="submit"]').click( function() {
    var words = $('#name').val().split(' ');
    alert(words.length);
});

回答by KXL

A slight improvement on other answers as is deals with more edge cases. ie using multiple spaces and punctuation together and also handles punctuation at the start and end of text properly.

对其他答案的轻微改进处理了更多的边缘情况。即一起使用多个空格和标点符号,并正确处理文本开头和结尾的标点符号。

var numOfWords = $('#name').val().replace(/^[\s,.;]+/, "").replace(/[\s,.;]+$/, "").split(/[\s,.;]+/).length;

It first strips off any punctuation and spaces at the beginning and end of the text and then counts what's left. Obviously one can add more punctuation (like exclamation marks) if required.

它首先去除文本开头和结尾的所有标点符号和空格,然后计算剩下的内容。显然,如果需要,可以添加更多标点符号(如感叹号)。

回答by Edu Lomeli

Is very useful to remove whitespaces from the beginning and end of the string usign $.trim(). You can use keyupevent for a realtimecounting.

从字符串的开头和结尾删除空格 usign 非常有用$.trim()。您可以使用keyup事件进行实时计数。

$('#name').keyup(function(){
    var words = $.trim($(this).val()).split(' ');
    console.log(words.length);
});

回答by Trevor

var str = $('#name').val(),
    count = str.split(' ').length;

Assuming that each word is seperated by a space

假设每个词之间用空格隔开

回答by charlybr

You can specify multiple characters to split your source string :

您可以指定多个字符来拆分源字符串:

$('input[type="submit"]').click( function() {
    var words = $('#name').val().split(/[\s\.,;]+/);
    console.log(words.length);
});

回答by Marco

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:

请参阅我开发的这个 jQuery 插件:

https://github.com/mcdesignpro/maxLenght

https://github.com/mcdesignpro/maxLenght

回答by Ketan Savaliya

Try this to count words in JavaScript using JQuery.

尝试使用 JQuery 计算 JavaScript 中的单词数。

$(document).ready(function(){
    $("button").click(function(){
        var words = $.trim($("#name").val()).split(" ");
        alert(words.length);
    });
});

See more @ Count words in JavaScript using JQuery

使用 JQuery查看更多@Count words in JavaScript