Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Arrays_Regex - Fatal编程技术网

Javascript 检查数组元素中的两个空格字符

Javascript 检查数组元素中的两个空格字符,javascript,arrays,regex,Javascript,Arrays,Regex,如何判断数组元素是否由三个单词组成(即,其中是否有两个空格字符)?它可能看起来像“abc def ghi”。我正试图在数组中搜索此表单的元素,并将删除此表单,而其他格式为“jkl xyz”或“jkl”的元素将保留在数组中。您可以使用类似以下的正则表达式: /^[^\s]+\s[^\s]+\s[^\s]+$/.test("abc def def") // true /^[^\s]+\s[^\s]+\s[^\s]+$/.test("abc def ") // false 这意味着: ^ Start

如何判断数组元素是否由三个单词组成(即,其中是否有两个空格字符)?它可能看起来像“abc def ghi”。我正试图在数组中搜索此表单的元素,并将删除此表单,而其他格式为“jkl xyz”或“jkl”的元素将保留在数组中。

您可以使用类似以下的正则表达式:

/^[^\s]+\s[^\s]+\s[^\s]+$/.test("abc def def") // true
/^[^\s]+\s[^\s]+\s[^\s]+$/.test("abc def ") // false
这意味着:

^ Start of string
[^\s]+ 1 or more none space characters
\s a space character
[^\s]+ 1 or more none space characters
\s a space character
[^\s]+ 1 or more none space characters
\s a space character
$ End of string

您可以将
搜索
功能与以下正则表达式一起使用:

str.search(/\b(\w+ \w+ \w+)\b/g);
详细阅读



正则表达式解释:

\w+ \w+ \w+
-----------

Match a single character that is a “word character” (ASCII letter, digit, or underscore only) «\w+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “ ” literally « »
Match a single character that is a “word character” (ASCII letter, digit, or underscore only) «\w+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “ ” literally « »
Match a single character that is a “word character” (ASCII letter, digit, or underscore only) «\w+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»

请共享您的代码-这样可以更轻松地调查您的问题所在
["jkl xyz", "gty slp"]
\w+ \w+ \w+
-----------

Match a single character that is a “word character” (ASCII letter, digit, or underscore only) «\w+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “ ” literally « »
Match a single character that is a “word character” (ASCII letter, digit, or underscore only) «\w+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “ ” literally « »
Match a single character that is a “word character” (ASCII letter, digit, or underscore only) «\w+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»