字符串中的空格是允许的,但不是在第一个或最后一个位置

Space in string allowed, but not at first or last position

本文关键字:第一个 最后一个 位置 空格 字符串      更新时间:2023-09-26

对于表单验证,我必须用javascript检查输入的有效名称字符串必须符合以下模式。

  • 我不能以空格开始或结束
  • 可以包含空格
  • 可以包含大写或小写字母,包括ê è等
  • 可能有像- ' "
  • 这样的符号
  • 必须包含至少1个字符

这个RegExp几乎完成了这个工作:

[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-]

但是这个RegExp不检查开始和结束处的空格。

哪个JS RegExp需要上面提到的要求?

Thanks in advance

以下是我对这个话题的看法:

if (subject.match(/^(?='S+)(?=[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-]*$).*(?='S).$/)) {
    // Successful match
}

它的意思是,至少从非空格开始。这里是条件1和条件5

然后确保整个内容仅由允许的字符组成。这是你所有的其他条件。

然后确保至少有一个非空格字符,匹配它,然后匹配结尾。

更多细节:

"
^                                                                                      # Assert position at the beginning of the string
(?=                                                                                    # Assert that the regex below can be matched, starting at this position (positive lookahead)
   'S                                                                                     # Match a single character that is a “non-whitespace character”
      +                                                                                      # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
(?=                                                                                    # Assert that the regex below can be matched, starting at this position (positive lookahead)
   [a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-]       # Match a single character present in the list below
                                                                                             # A character in the range between “a” and “z”
                                                                                             # A character in the range between “A” and “Z”
                                                                                             # One of the characters “àáâäãåèéêëìíîïòóôöõøùúûüÿýñçcšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆCŠŽ?ð ,.”
                                                                                             # The character “'”
                                                                                             # The character “-”
      *                                                                                      # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   $                                                                                      # Assert position at the end of the string (or before the line break at the end of the string, if any)
)
.                                                                                      # Match any single character that is not a line break character
   *                                                                                      # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?=                                                                                    # Assert that the regex below can be matched, starting at this position (positive lookahead)
   'S                                                                                     # Match a single character that is a “non-whitespace character”
)
.                                                                                      # Match any single character that is not a line break character
$                                                                                      # Assert position at the end of the string (or before the line break at the end of the string, if any)
"

您需要使用RegExp ^和$代码,它们分别指定开始和结束。

请参阅有关此操作的更多文档

希望这对你有帮助!

试试这个

^(?! )[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-]*[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð,.'-]$

在Regexr上查看

^将模式锚定到字符串

的开头

$将模式锚定到字符串

的末尾

(?! )是一个负向前看,它确保它不以空格

开始。

然后是您的字符类与*量词,意味着0或更多次。最后还是你的类,但是没有空格,这是为了确保它不会以空格结束。

很遗憾Javascript正则表达式不支持Unicode,并且不允许所有类型的字母使用'p{L}

相关文章: