简体   繁体   English

名称不能超过一个空格字符的正则表达式

[英]Regular expression for a name that can contain not more than one space character

I'm struggling to find a regular expression that matches a name that can contain a space in it.我正在努力寻找与其中可以包含空格的名称匹配的正则表达式。 So far this is what I've tried:到目前为止,这是我尝试过的:

^(?![\s]+$)(?![\s]{2,})[a-zA-Z\s]{2,25}$

Unfortunately I'm not so good on regex.不幸的是,我对正则表达式不太擅长。 The name can contain only letters and optionally a space character, and if a space is present there must be no more than one space character in the word.名称只能包含字母和可选的空格字符,如果存在空格,则单词中的空格字符不得超过一个。 The whole name length (counting also the space) must be between 2 and 25 characters long.整个名称长度(包括空格)必须在 2 到 25 个字符之间。 Here some examples of names that have to match:下面是一些必须匹配的名称示例:

  • Some Name一些名字
  • SingleWordName单字名

Here some examples of names that have not to match the regular expression:下面是一些不必匹配正则表达式的名称示例:

  • (a name full a space characters) (名称全是空格字符)
  • Some Name (two spaces between Some and Name) Some Name(Some 和 Name 之间有两个空格)

The last requirement is that I have to use this regular expression both in javascript and php.最后一个要求是我必须在 javascript 和 php 中都使用这个正则表达式。

Thanks in advance!提前致谢!

The pattern ^(??[\s]+$)(,,[\s]{2,})[a-zA-Z\s]{2,25}$ that you tried matches:您尝试的模式^(??[\s]+$)(,,[\s]{2,})[a-zA-Z\s]{2,25}$匹配:

  • Assert that the string does not consist of 1 or more whitespace chars ^(?![\s]+$)断言字符串不包含 1 个或多个空格字符^(?![\s]+$)
  • Asserts not 2 or more whitespace chars at the beginning (?,[\s]{2,})断言开头不是 2 个或更多空白字符(?,[\s]{2,})
  • Match 2-25 chars being either a-zA-Z or a whitespace char [a-zA-Z\s]{2,25}$匹配 2-25 个字符为 a-zA-Z 或空白字符[a-zA-Z\s]{2,25}$

There is no restriction to match a single space没有限制匹配单个空格 in the whole string在整个字符串中

Note that \s could also match a newline or a tab.请注意, \s也可以匹配换行符或制表符。


What you could do is assert 2-25 characters in the string.您可以做的是在字符串中声明 2-25 个字符。

Then match 1+ chars a-zA-Z and optionally match a single space and 1+ chars a-zA-Z然后匹配 1+ 字符 a-zA-Z 并可选择匹配单个空格和 1+ 字符 a-zA-Z

^(?=.{2,25}$)[a-zA-Z]+(?: [a-zA-Z]+)?$

The pattern matches:模式匹配:

  • ^ Start of string ^字符串开头
  • (?=.{2,25}$) Positive lookahead, assert 2-25 chars in the string (?=.{2,25}$)正向前瞻,在字符串中断言 2-25 个字符
  • [a-zA-Z]+ Match 1+ chars A-Za-z [a-zA-Z]+匹配 1+ 个字符 A-Za-z
  • (?: [a-zA-Z]+)? Optionally match可选匹配 and again 1+ chars from the character class再从字符 class 中提取 1+ 个字符
  • $ End of string $字符串结尾

See a regex demo .查看正则表达式演示

You could match the string with the following regular expression.您可以将字符串与以下正则表达式匹配。

^(?!.* .* )[a-zA-Z ]{2,25}$

Demo演示

The elements of the expression are as follows.表达式的元素如下。

^                # match beginning of string
(?!.* .* )       # negative lookahead asserts the string does not
                 # contain two (or more) spaces
[a-zA-Z ]{2,25}  # match between 2 and 25 letters and spaces
$                # match end of string

暂无
暂无

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

相关问题 使用正则表达式(JavaScript)删除一个空格字符 - Remove One Space Character with a Regular Expression (JavaScript) 寻找正则表达式以验证密码,长度必须大于8个字符,并且至少包含一个特殊字符 - looking for a regular expression to validate a password, must be greater than 8 characters long and contain at least one special character 正则表达式,至少一个字符和一个数字,重复不超过两个 - Regular Expression for atleast one character and one number with no repeat more than two 正则表达式删除多个连续的“-” - Regular expression to remove more than one continuous “--” 正则表达式不超过一位数 - regular expression no more than one digit 正则表达式检查任何字符超过两次 - Regular expression to check any character in more than two times Javascript正则表达式在开始处仅允许一个加号(+),并且字符串的长度不应超过15个字符数 - Javascript Regular Expression allow only one plus (+) sign at start and length of string should not more than 15 character number only 如何在 select2 数据行中的一行中显示多个空格字符? - How can I show more than one white space character in a row in a select2 data row? javascript正则表达式允许名称带有一个空格和特殊字母 - javascript regular expression allow name with one space and special Alphabets 正则表达式仅匹配字符或空格或两个单词之间的一个点,不允许双倍空格 - Regular expression to match only character or space or one dot between two words, no double space allowed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM