-2

I am trying to implement a regex rule that would allow users to enter white spaces, but it should not be all white spaces, nor begin or end with a white space. My research did not yield any workable solution. Currently, my rule looks like this, and I would like to add the white space requirement on top of that:

let string_rule = "^[a-zA-Z0-9_-]{3,16}$";
Yugue Chen
  • 79
  • 5

2 Answers2

0

let string_rule = "^\S[a-zA-Z0-9_-]{3,16}\S$";

Shane
  • 66
  • 4
0

you can use following regex:

^(?:\S.*?\S|\S)$

Regex Demo

Details

^: start of string

\S.*?\S: first state which your input have more than 1 character, then its first and last character must exist and not to be spaces

\S: second state when your input consists of only one character

$: end of string

mjrezaee
  • 1,100
  • 5
  • 9