Javascript:检查用户的输入文本是否为“空白语言”

时间:2016-04-12 19:55:26

标签: javascript jquery html

有些人类语言用白色空格切割它的单词,比如说空白语言:

          e.g. English: I[whitespace]love[whitespace]you.

还有一些人类语言,其中的角色不会被白色空间切割,比方说,非白空间语言:

          e.g. Chinese: 我爱你

我想知道如何检查用户的输入文本是否为空格语言。

正如建议的那样,澄清问题的本质:

检查的标准不应该是检查文本中是否经常出现空白,因为这种方法会错误地将“IloveU”视为非空白语言。

1 个答案:

答案 0 :(得分:0)

var sentence = "This is a test sentence";
var searched = sentence.search(" "); // Returns the first index of the white space

if((searched !=null)&&(searched >0))
{
  alert("It is a white-space language");
}
else
{
   alert("It is not a white-space language")
}
  

您还可以使用:

var matched = sentence.match(" "); // Returns the array of the white space
if((matched !=null)&&(matched.length > 0))
{
   alert("It is a white-space language");
}
else
{
   alert("It is not a white-space language")
}