Regex to prevent whitespace at the front and end on a variable

ashfaq
Tera Contributor

Hi, I am looking for the correct Regex to prevent a space at the start and end of a 'Single Line Text' variable. I have used the following:

^\S+$

But this allows a space at the start at end but NOT in the middle. I need to restrict a space at the start and the end but allow a space in the middle. 

 

I have tried multiple different regex values to no avail. Any help would be appreciated. 

1 REPLY 1

Hemant Goldar
Mega Sage
Mega Sage

Hi @ashfaq ,

please use the below Regex key.
^\S(?:.*\S)?$


I hope this helps!

 

Regards,

Hemant 

**Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.**