javascript 正则表达式检查两个连续的空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8966884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular expression checking for two consecutive spaces?
提问by user1127051
I am trying to validate the following condition:
Characters allowed, Max length of 5: A-Z, 0-9, space and ('-
and this is what I have got so far:
我正在尝试验证以下条件:
允许的字符数,最大长度为 5:AZ、0-9、空格和 ('-
这是我到目前为止所得到的:
/^[a-zA-Z,\d,\-,\(,\']{1,5}$/;
How do I check for two consecutive spaces?
如何检查两个连续的空格?
回答by sblom
Probably match it again against /\s\s/
.
可能会再次与 匹配/\s\s/
。
回答by Ricketyship
You can check for two consecutive spaces by using the repetition regex. i.e If you want to match a regex which repeats say between 1 to 12 times, you can give,
您可以使用重复正则表达式检查两个连续的空格。即如果你想匹配一个重复 1 到 12 次的正则表达式,你可以给,
regex{1, 12}
Similarly, if u want to match a space which repeats just two times and not more or less than that, you can give
同样,如果你想匹配一个只重复两次且不超过或少于两次的空格,你可以给出
\s{2}
Remember that this is a general way of checking the repeat patterns. The numbers in curly braces will always try to see the number of repetitions which the previous regex has.
请记住,这是检查重复模式的一般方法。花括号中的数字将始终尝试查看前一个正则表达式的重复次数。
cheers!
干杯!
回答by stema
So my assumption, you want to allow space characters, but want to disallow consecutive spaces (you don't make it clear in which way you want to check for them).
所以我的假设是,您希望允许空格字符,但希望禁止连续空格(您没有明确说明要以哪种方式检查它们)。
You could achieve this with a negative lookahead.
您可以通过负前瞻来实现这一点。
^(?!.* )[a-zA-Z\d(' -]{1,5}$
Just add the space to the character class and use the zero width negative lookahead assertion to ensure that the expression will fail, if there are two consecutive space characters.
只需将空格添加到字符类并使用零宽度负前瞻断言来确保表达式将失败,如果有两个连续的空格字符。
See it here on Regexr
在 Regexr 上看到它
Btw. I removed the commas from your character class most of the escaping and moved for that reason the hyphen to the end of the class.
顺便提一句。我从你的字符类中删除了大部分转义的逗号,因此将连字符移到类的末尾。
回答by Zachary Ryan Smith
\s{2}
is close, but \s
matches many types of whitespace. ' {2}' (the pretty code format took away the space) works for me.
\s{2}
很接近,但\s
匹配多种类型的空格。“{2}”(漂亮的代码格式占用了空间)对我有用。
回答by James Jithin
This would be something you can use:
这将是您可以使用的东西:
var string = "SD D";
if (string.match(/^[A-Z\d\s\(\-']{1,5}$/)) {
alert(string.match(/\s{2,}/) != null?"Has consecutive spaces of length 2+" : "Does not have consecutive spaces");
}
回答by Borodin
You don't need the commas in your regex, and they will cause it to misbehave. Write instead
你的正则表达式中不需要逗号,它们会导致它行为不端。改写
/^[a-zA-Z\d\-\(\']{1,5}$/
I'm not sure what you mean about two consecutive spaces, as it doesn't seem to apply to the rest of the question. A regex like / /
will match a pair of spaces, or /\x20\x20/
or /[ ][ ]/
to make them more visible.
我不确定您对两个连续空格的意思,因为它似乎不适用于问题的其余部分。一个正则表达式像/ /
将匹配一对空间,或者/\x20\x20/
或者/[ ][ ]/
使他们更加明显。