简体   繁体   English

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

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

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

try this: 试试这个:

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

EDIT1 EDIT1

/^(\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:如果你需要一些像它之间的空格或点那样

/^([\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

EDIT2 Explanation: EDIT2说明:

may be problem in \\s = [\\r\\n\\t\\f ] so if only space allowed - \\s? 在\\ s = [\\ r \\ n \\ t \\ f]中可能有问题,所以如果只允许空格 - \\s? can be replaced with [ ]? 可以用[ ]?代替[ ]?

http://regex101.com/r/wV4yY5 http://regex101.com/r/wV4yY5

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"

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. 点在第二个和任何连续的单词。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM