![](/img/trans.png)
[英]javascript: how can I split this command line string using spaces but args contain spaces
[英]How can I determine if a string only contains spaces, using javascript?
如何使用javascript确定输入字符串是否只包含空格?
另一个好帖子: 更快的JavaScript修剪
您只需要应用trim
函数并检查字符串的长度。 如果修剪后的长度为0 - 则字符串仅包含空格。
var str = "data abc";
if((jQuery.trim( str )).length==0)
alert("only spaces");
else
alert("contains other characters");
if (!input.match(/^\s*$/)) {
//your turn...
}
或者,您可以执行一个test()
,它返回一个布尔值而不是一个数组
//assuming input is the string to test
if(/^\s*$/.test(input)){
//has spaces
}
if(!input.match(/^([\s\t\r\n]*)$/)) {
blah.blah();
}
最快的解决方案是使用正则表达式原型函数test()并查找任何不是空格或换行符的字符\\S
:
if (/\S/.test(str))
{
// found something other than a space or a line break
}
如果你有一个超长字符串,它可以产生显着的差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.