Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Regex来计算字符串中的总字数_Javascript - Fatal编程技术网

Javascript Regex来计算字符串中的总字数

Javascript Regex来计算字符串中的总字数,javascript,Javascript,此字符串中的总字数为11。但是我的代码返回13 var txt = "Helllo, my -! This is a great day to say helllo.\n\n\tHelllo! 2 3 4 23"; txt = txt.replace(/[0-9]/g, ''); var words_count = txt.match(/\S+/g).length; \S+将匹配任何非空格字符,其中包括子字符串,如-。您可以将其中至少包含一个字母的非空格字符序列与\S*[a-z]\S*匹配:

此字符串中的总字数为11。但是我的代码返回13

var txt = "Helllo, my -! This is a great day to say helllo.\n\n\tHelllo! 2 3 4 23";
txt = txt.replace(/[0-9]/g, '');
var words_count = txt.match(/\S+/g).length;

\S+
将匹配任何非空格字符,其中包括子字符串,如
-。您可以将其中至少包含一个字母的非空格字符序列与
\S*[a-z]\S*
匹配:

var txt=“Helllo,我的-!今天是说Helllo的好日子。\n\n\thello!2 3 4 23”;

console.log(txt.match(/\S*[a-z]\S*/gi).length)你对
单词的定义是什么?双引号是故意的吗?(
)([“helllo”、“my”、“this”、“is”、“a”、“great”、“day”、“to”、“say”、“helllo”、“helllo”]是11个单词)我错加了双引号,但输入中只有一个单词。那也是打字错误吗?