关于javascript:允许在字符串中使用空格,但不能在第一个或最后一个位置

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

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

  • 我可能不会以空格开头或结尾
  • 它可能包含空格
  • 它可能包含大写小写字母,包括?a?
  • 它的符号可能像-\\'"
  • 它必须包含至少1个字符

此RegExp几乎可以完成这项工作:

1
[a-zA-Z?????¤???¨???a?????-???ˉ?2?3????μ???1?o?????????±?§?????????????????????????????????"?"??????????????????????????????a???° ,.'-]

但是此RegExp不会在结尾处检查空格。

哪些JS RegExp需要上述要求?

预先感谢


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

1
2
3
if (subject.match(/^(?=\\S+)(?=[a-zA-Z????¢?¤?£?¥?¨???a?????-???ˉ?2?3?′???μ???1?o?????????±?§???????€?????????…???‰?????????????’?"?"?–?????????????????‘?????’???????a???° ,.'-]*$).*(?=\\S).$/)) {
    // Successful match
}

它基本上说,至少从一个不是空格的东西开始。因此这里是条件1和5。

然后确保整个内容仅包含允许的字符。这是您所有其他条件的前提。

然后确保至少有一个非空格字符,将其匹配然后匹配末尾。

更多详细信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"
^                                                                                      # 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 a€?non-whitespace charactera€?
      +                                                                                      # 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????¢?¤?£?¥?¨???a?????-???ˉ?2?3?′???μ???1?o?????????±?§???????€?????????…???‰?????????????’?"
?"?–?????????????????‘?????’???????a???° ,.'-]       # Match a single character present in the list below
                                                                                             # A character in the range between a€?aa€? and a€?za€?
                                                                                             # A character in the range between a€?Aa€? and a€?Za€?
                                                                                             # One of the characters a€?????¢?¤?£?¥?¨???a?????-???ˉ?2?3?′???μ???1?o?????????±?§c?????€?????????…???‰?????????????’?"
?"?–?????????????????‘?????’??C?????° ,.a€?
                                                                                             # The character a€?'a€?
                                                                                             # The character a€?-a€?
      *                                                                                      # 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 a€?non-whitespace charactera€?
)
.                                                                                      # 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
^(?! )[a-zA-Z?????¤???¨???a?????-???ˉ?2?3????μ???1?o?????????±?§?????????????????????????????????"?"??????????????????????????????a???° ,.'-]*[a-zA-Z????¢?¤?£?¥?¨???a?????-???ˉ?2?3?′???μ???1?o?????????±?§???????€?????????…???‰?????????????’?"?"?–?????????????????‘?????’???????a???°,.'-]$

在Regexr上查看它

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

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

(?! )是一个否定的超前行为,可确保其不以空格开头

然后在您的角色类后加上*量词,表示0次或多次。最后,再次有您的课程,但是没有空格,这是为了确保它不会以空格结尾。

可惜Javascript regexes不支持Unicode,并且不允许\\p{L}用于所有类型的字母。


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

请参阅有关此内容的更多文档。

希望这会有所帮助!