Javascript 检查数组元素中的两个空格字符
如何判断数组元素是否由三个单词组成(即,其中是否有两个空格字符)?它可能看起来像“abc def ghi”。我正试图在数组中搜索此表单的元素,并将删除此表单,而其他格式为“jkl xyz”或“jkl”的元素将保留在数组中。您可以使用类似以下的正则表达式: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
/^[^\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) «+»