Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.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正则表达式,用于计算空格,但不计算制表符或回车_Javascript_Regex - Fatal编程技术网

JavaScript正则表达式,用于计算空格,但不计算制表符或回车

JavaScript正则表达式,用于计算空格,但不计算制表符或回车,javascript,regex,Javascript,Regex,我希望计算字符串中的空格数量,但是我的正则表达式模式似乎包括制表符和回车符。如何更正下面的代码,使其只计算我不想在模式中使用的空格 "This is a string from a textarea on a form... . . ".match(/\s/g).length 这个怎么样 "This is a string from a textarea on a form... . . ".match(/\u0020/g).length \u0020是空间的unicode 我其实只想数数单

我希望计算字符串中的空格数量,但是我的正则表达式模式似乎包括制表符和回车符。如何更正下面的代码,使其只计算我不想在模式中使用的空格

"This is a string from a textarea on a form... . . ".match(/\s/g).length
这个怎么样

"This is a string from a textarea on a form... . . ".match(/\u0020/g).length
\u0020是空间的unicode


我其实只想数数单词是的

您可以试试\w+

例如:

console.log("This is a".match(/\w+/g).length);
您可以使用\s+或[]+基于一个或多个空格进行拆分


注意:确保\s匹配任何空白字符[\r\n\t\f]

您希望得到什么答案以及您得到什么try str.match//g.length如果单词之间有多个空格,则此字符串有多少空格。你只想数单词吗?我只想数单词是的,我不想在我的模式中使用。请问为什么?您也可以使用\x20,这里不需要使用unicode。您选择\x20而不是\u0020的原因是什么?