0

My target is to improve my regex. Regex need to everything except some special charaters, not to allow empty spaces at start, and not to allow empty spaces at the end.

^(?!\s*$)[^-\s][^`=~!@#$%^&*()[\]\/\\{}"|<>?]{3,100}$

Example:

word valid

word [space] invalid

[space] word invalid

word w valid

My regex did everything except empty space at the end. How to add this condition to forbit empty spaces at the end of regex?

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
IntoTheDeep
  • 3,744
  • 13
  • 38
  • 76

1 Answers1

0

You may add another negative lookahead to disallow space at the end:

^(?!\s*$)(?![^]*\s$)(?![-\s])[^`=~!@#$%^&*()[\]\/\\{}"|<>?]{3,100}$

(?![^]*\s$) is negative lookahead to assert that your regex won't allow a space at the end.

RegEx Demo

anubhava
  • 713,503
  • 59
  • 514
  • 593