Regex - match a string without leading and trailing spaces
depending on your regex engine supporting look around
^(?=[A-Za-z0-9])([A-Za-z0-9\s]*)(?<=[A-Za-z0-9])$
Demo
You could use lookarounds to ensure that there are no spaces at both ends:
^(?! )[A-Za-z0-9 ]*(?<! )$
Live demo
But if the environment doesn't support lookarounds the following regex works in most engines:
^[A-Za-z0-9]+(?: +[A-Za-z0-9]+)*$