正则表达式在开始或结束时没有空白区域,但允许中间空白,空白和任何6-20个字符?

Chr*_* So 5 regex

^$|^[^\s]+(\s+[^\s]+)*$用来实现:

  1. 开始或结束时没有空白区域允许白色
  2. 中间的空间
  3. 空字符串

但是如何将量词限制在6-20之间?

以下内容应该通过

""              <-- (empty string)
"??"          <-- ( any character)
"A B"          <-- (allow space in middle)
"hi! Hello There"
Run Code Online (Sandbox Code Playgroud)

以下应该失败

"A"            <-- (less than 2 number of characters)
" AB"          <-- (space at the start)
"AB "          <-- (space at the end)
" AB "
"test test test test test test"  <--- (more than 20 characters included spaces)
Run Code Online (Sandbox Code Playgroud)

谢谢!

anu*_*ava 4

您可以使用这个正则表达式:

^(?:\S.{4,18}\S)?$
Run Code Online (Sandbox Code Playgroud)

工作演示