我需要过滤一个仅包含空格的字段,类似于:
if (word == /((\s)+)/ ) return 'no name'
但它不起作用...还有其他的想法吗?谢谢你的建议!
我需要过滤一个仅包含空格的字段,类似于:
if (word == /((\s)+)/ ) return 'no name'
但它不起作用...还有其他的想法吗?谢谢你的建议!
if(/^\s+$/.test(word))
。(注意 ^
和 $
,没有它们,正则表达式适用于任何至少包含一个空格字符的字符串)。您也可以使用!word.trim()
进行相同的检查,完全忽略正则表达式。
只需使用空格字符space
代替\s
即可。
' '.match(/ +/)
''.match(/ +/)
输出
[" ", index: 0, input: " ", groups: undefined]'
null
if (word.replace(/\s+/, "") === "") {
console && console.log("empty string found!");
return 'no name';
}
console & ...
没有意义,而 word
周围的括号是多余的。 - melpomene