Regular expression for no white space at start or end, but allow white space in middle, empty and any 6-20 characters?

Chris So

I use ^$|^[^\s]+(\s+[^\s]+)*$ to achieve:

  1. no white space at start or end allow white
  2. space in middle
  3. empty string

But how can I put the quantifiers to limit character count in between 6 - 20?

The following should pass

""              <-- (empty string)
"中文"          <-- ( any character)
"A B"          <-- (allow space in middle)
"hi! Hello There"

The following should fail

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

Thanks!

anubhava

You can use this regex:

^(?:\S.{4,18}\S)?$

Working Demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regular expression - String cannot start, end with white-space and consist of few white-spaces in a row

From Dev

regular expression not matching white space

From Dev

How to generate Regular Expression that doesn't contain any white space at all and even at the beginning or the end of the string

From Dev

Javascript regex - no white space at beginning + allow space in the middle

From Dev

Javascript regex - no white space at beginning + allow space in the middle

From Dev

RegEx for expression between 2 white space characters

From Dev

I need a regular expression to remove white space in the beginning and it should allow spaces in between two words

From Dev

Regular expression - allow space and any number of digits

From Dev

What is the regular expression to replace white space with a specified character?

From Dev

Regular expression to ignore white space from html tag content

From Dev

Regular expression to ignore white space from html tag content

From Dev

Regex for empty string or white space

From Dev

White empty space below Canvas

From Dev

White empty space below Canvas

From Dev

remove the empty white space in previous expression in ssrs report

From Dev

CCLabelTTF::setString not accepting white space at start and end of string

From Dev

regular expression for is empty space

From Dev

how to have regular expression for a textfield which accepts all characters except a comma (,) and do not accept a white space at both ends

From Dev

Trimming white space - front end or back end?

From Dev

Trimming white space - front end or back end?

From Dev

Regular Expression Match Doesn't Start With, Contain, Or End With Space And Not Empty String

From Dev

not allow 2 white space in text angularjs

From Dev

detecting white space characters, \t, \n, etc

From Dev

Remove white space and special characters in java

From Dev

remove white space from the end of line in linux

From Dev

Remove white space at the end of text in Excel

From Dev

Remove white space from beginning and end of LINES

From Dev

How to remove white space at the end of wordpress website?

From Dev

preg_match expression including white space

Related Related

  1. 1

    Regular expression - String cannot start, end with white-space and consist of few white-spaces in a row

  2. 2

    regular expression not matching white space

  3. 3

    How to generate Regular Expression that doesn't contain any white space at all and even at the beginning or the end of the string

  4. 4

    Javascript regex - no white space at beginning + allow space in the middle

  5. 5

    Javascript regex - no white space at beginning + allow space in the middle

  6. 6

    RegEx for expression between 2 white space characters

  7. 7

    I need a regular expression to remove white space in the beginning and it should allow spaces in between two words

  8. 8

    Regular expression - allow space and any number of digits

  9. 9

    What is the regular expression to replace white space with a specified character?

  10. 10

    Regular expression to ignore white space from html tag content

  11. 11

    Regular expression to ignore white space from html tag content

  12. 12

    Regex for empty string or white space

  13. 13

    White empty space below Canvas

  14. 14

    White empty space below Canvas

  15. 15

    remove the empty white space in previous expression in ssrs report

  16. 16

    CCLabelTTF::setString not accepting white space at start and end of string

  17. 17

    regular expression for is empty space

  18. 18

    how to have regular expression for a textfield which accepts all characters except a comma (,) and do not accept a white space at both ends

  19. 19

    Trimming white space - front end or back end?

  20. 20

    Trimming white space - front end or back end?

  21. 21

    Regular Expression Match Doesn't Start With, Contain, Or End With Space And Not Empty String

  22. 22

    not allow 2 white space in text angularjs

  23. 23

    detecting white space characters, \t, \n, etc

  24. 24

    Remove white space and special characters in java

  25. 25

    remove white space from the end of line in linux

  26. 26

    Remove white space at the end of text in Excel

  27. 27

    Remove white space from beginning and end of LINES

  28. 28

    How to remove white space at the end of wordpress website?

  29. 29

    preg_match expression including white space

HotTag

Archive