javascript 正则表达式仅匹配字符或空格或两个单词之间的一个点,不允许使用双空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24007489/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:58:38  来源:igfitidea点击:

Regular expression to match only character or space or one dot between two words, no double space allowed

javascriptregex

提问by user3291547

I need help with regular expression.I need a expression in JavaScript which allows only character or space or one dot between two words, no double space allowed.

我需要正则表达式方面的帮助。我需要一个 JavaScript 表达式,它只允许字符或空格或两个单词之间的一个点,不允许使用双空格。

I am using this

我正在使用这个

var regexp = /^([a-zA-Z]+\s)*[a-zA-Z]+$/;

but it's not working.

但它不起作用。

Example

例子

1.  hello space .hello - not allowed
2.  space hello space - not allowed

采纳答案by Subdigger

try this:

试试这个:

^(\s?\.?[a-zA-Z]+)+$

EDIT1

编辑1

/^(\s{0,1}\.{0,1}[a-zA-Z]+)+$/.test('space ..hello space')
false
/^(\s{0,1}\.{0,1}[a-zA-Z]+)+$/.test('space .hello space')
true

v2:

v2:

/^(\s?\.?[a-zA-Z]+)+$/.test('space .hello space')
true
/^(\s?\.?[a-zA-Z]+)+$/.test('space ..hello space')
false

v3: if you need some thisn like one space or dot between

v3:如果你需要一些 this 那么像一个空格或点之间

/^([\s\.]?[a-zA-Z]+)+$/.test('space hello space')
true
/^([\s\.]?[a-zA-Z]+)+$/.test('space.hello space')
true
/^([\s\.]?[a-zA-Z]+)+$/.test('space .hello space')
false

v4:

v4:

/^([ \.]?[a-zA-Z]+)+$/.test('space hello space')
true
/^([ \.]?[a-zA-Z]+)+$/.test('space.hello space')
true
/^([ \.]?[a-zA-Z]+)+$/.test('space .hello space')
false
/^([ ]?\.?[a-zA-Z]+)+$/.test('space .hello space')
true

EDIT2Explanation:

EDIT2解释:

may be problem in \s = [\r\n\t\f ] so if only space allowed - \s?can be replaced with [ ]?

\s = [\r\n\t\f ] 中可能有问题,所以如果只允许空格 -\s?可以替换为[ ]?

http://regex101.com/r/wV4yY5

http://regex101.com/r/wV4yY5

回答by gwcoffey

This regular expression will match multiple spaces or dots between words and spaces before the first word or after the last word. This is the opposite of what you want, but you can always invert it (!foo.match(...)):

此正则表达式将匹配单词之间的多个空格或点以及第一个单词之前或最后一个单词之后的空格。这与您想要的相反,但您始终可以反转它 ( !foo.match(...)):

/\b[\. ]{2,}\b|^ | $/

In regex101.com: http://regex101.com/r/fT0pF2

在regex101.com:http://regex101.com/r/fT0pF2

And in plainer english:

用更简单的英语:

\b        => a word boundary
[\. ]     => a dot or a space
{2,}      => 2 or more of the preceding
\b        => another word boundary
|         => OR
^{space}  => space after string start
|         => OR
{space}$  =>  space before string end

This will match:

这将匹配:

"this  that" // <= has two spaces
"this. that" // <= has dot space
" this that" // <= has space before first word
"this that " // <= has space after last word

But it will not match:

但它不会匹配:

"this.that and the other thing"

回答by j3ny4

How about that?

那个怎么样?

^\s*([a-zA-Z]+\s?\.?[a-zA-Z]+)+$

This allows:

这允许:

  1. Multiple leading spaces.
  2. Space as a delimiter.
  3. Dot in the second and any consecutive word.
  1. 多个前导空格。
  2. 空格作为分隔符。
  3. 点在第二个和任何连续的单词中。

回答by user2303197

Try this (only a single whitespace or a period is allowed between the words):

试试这个(单词之间只允许有一个空格或句号):

> /\w+[\s\.]\w+/.test('foo bar')
true
> /\w+[\s\.]\w+/.test('foo.bar')
true
> /\w+[\s\.]\w+/.test('foo..bar')
false
> /\w+[\s\.]\w+/.test('foo .bar')
false