-1

Im trying to add a length requirement to the below code.

Code: ^[a-zA-z0-9\!\$\%\&]+(?: [a-zA-z0-9\!\$\%\&]+)*$

I want a sentence with spaces to only have around 1 to 10 characters. I want to count spaces as well. The code provided doesn't allow leading or trailing space but space between.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
bryan3045
  • 1
  • 2
  • Should it also match a single space or consecutive spaces? Can you add which tool or language you are using? – The fourth bird Oct 30 '19 at 13:13
  • most regex use `{m,n}` to specify a min and max length for a pattern. For example, `[0-9]{3,5}` matches 3 to 5 digits. – BurnsBA Oct 30 '19 at 13:21
  • Can the sentence have multiple spaces? Can the spaces be consecutive? – Aplet123 Oct 30 '19 at 13:25
  • the sentence can have multiple spaces. I know about using {1,10} for length but i cant seem to wrap my expression the right way to have it work. its javascript by the way. – bryan3045 Oct 30 '19 at 13:49

1 Answers1

0

A sentence limited to 10 chars is obscenely small but you can use:

^(?=^.{1,10}$)[a-zA-z0-9\!\$\%\&]+(?: [a-zA-z0-9\!\$\%\&]+)*$

(?=^.{1,10}$) = ensure that between 1 and 10 chars exist between the start and end of the string.

https://regex101.com/r/zsa1N9/1


I see your new comment about Javascript. You would be better off adding a check for .length in addition to your regex.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77