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

时间:2011-11-30 11:39:14

标签: javascript regex expression

对于表单验证,我要使用javascript检查输入是否有效 字符串必须符合以下模式。

  • 我可能不会以空格开头或结尾
  • 可能包含空格
  • 它可能包含大写小写字母,包括êèensuch
  • 它可能符号 - '“
  • 必须包含至少1个字符

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

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

但是这个RegExp不会在开始时检查空格。

哪个JS RegExp需要上述要求?

提前致谢

3 个答案:

答案 0 :(得分:1)

以下是我对该主题的看法:

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)
"

答案 1 :(得分:0)

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

请参阅more documentation

希望这有帮助!

答案 2 :(得分:0)

试试这个

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

here on Regexr

^将模式锚定到字符串的开头

$将模式锚定到字符串的末尾

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

然后你的角色类跟随*量词,意味着0次或更多次。最后再次上课,但没有空格,这是为了确保它不会以空格结束。

遗憾的是,Javascript正则表达式没有Unicode支持,并且不允许\p{L}用于所有类型的字母。