首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在撇号和连字符之前或之后禁止任何空格

在撇号和连字符之前或之后禁止任何空格
EN

Stack Overflow用户
提问于 2020-12-12 17:15:48
回答 2查看 265关注 0票数 1

我需要这个要求的详细验证:

  1. 接受字母表(a或a)
  2. 可能包括这一特殊的查尔(奥塞耶内埃瓦·努伊内勒)*美元
  3. 接受空格、撇号和连字符
  4. 可以在非空格字符之间使用撇号和连字符,例如:
  • Le'brahm
  • Ben-John

不能接受Le' brahmBen -JohnBen- John

我目前使用此regex,但不能完全填充数字4的要求和数字3的部分。

代码语言:javascript
复制
^[a-zA-Z åçêëèïîìæôöòûùÿáíóúñ]*$

如果我像这样加催眠药

代码语言:javascript
复制
^[a-zA-Z -'åçêëèïîìæôöòûùÿáíóúñ]*$

正则表达式变成错误,它接受数字字符(不应该是)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-12 17:22:56

您可以试试这个正则表达式:

代码语言:javascript
复制
^[a-zA-Zåçêëèïîìæôöòûùÿáíóúñ]+(?:[-' ][a-zA-Zåçêëèïîìæôöòûùÿáíóúñ]+)*$

RegEx演示

RegEx详细信息:

  • [a-zA-Zåçêëèïîìæôöòûùÿáíóúñ]+:匹配[...]中给定字母的1+
  • (?:[-' ][a-zA-Zåçêëèïîìæôöòûùÿáíóúñ]+)*:匹配由-'或空格分隔的同一组字符中的0或多个。
票数 2
EN

Stack Overflow用户

发布于 2020-12-12 21:10:04

使用观景台:

代码语言:javascript
复制
^(?!.*[-'‘’ ]{2})(?![-'‘’ ])(?!.*[-'‘’ ]$)[a-zA-Zåçêëèïîìæôöòûùÿáíóúñ '‘’-]+$

证明

解释

代码语言:javascript
复制
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [-‘’' ]{2}               any character of: '-', ''', ' ', '‘', '’' (2
                             times)
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    [-'‘’ ]                    any character of: '-', ''', ' ', '‘', '’'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [-'‘’ ]                    any character of: '-', ''', ' ', '‘', '’'
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [a-zA-                    any character of: 'a' to 'z', 'A' to 'Z',
  Zåçêëèïîìæôöòûùÿáíóú     'å', 'ç', 'ê', 'ë', 'è', 'ï', 'î', 'ì',
  ñ '‘’-]+                   'æ', 'ô', 'ö', 'ò', 'û', 'ù', 'ÿ', 'á',
                           'í', 'ó', 'ú', 'ñ', ' ', ''', '-', '‘', '’' (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65267680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档