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
  • 607,720
  • 39
  • 448
  • 563
IntoTheDeep
  • 4,027
  • 15
  • 39
  • 84

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
  • 761,203
  • 64
  • 569
  • 643