Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

What is the correct Regular Expression for preventing a space in a variable?

Zhane
Tera Contributor

I am trying to prevent any spaces on a variable and set up a regex for this and it's not working

find_real_file.png

1 ACCEPTED SOLUTION

Shane41
ServiceNow Employee
ServiceNow Employee

Hi Zhane,

I would recommend changing your regex to ^\S+$

Have you added the regex to the variable itself? (Note:It will only work on particular variable types)

There is a field on the variable that needs to be set to your new regex record - Validation regex (you may need to add this to the form layout)

find_real_file.png

I created the updated regex (^\S+$) in my own instance and it works after I added it to a single line text variable

find_real_file.png

Hope this helps,

Shane

View solution in original post

2 REPLIES 2

Shane41
ServiceNow Employee
ServiceNow Employee

Hi Zhane,

I would recommend changing your regex to ^\S+$

Have you added the regex to the variable itself? (Note:It will only work on particular variable types)

There is a field on the variable that needs to be set to your new regex record - Validation regex (you may need to add this to the form layout)

find_real_file.png

I created the updated regex (^\S+$) in my own instance and it works after I added it to a single line text variable

find_real_file.png

Hope this helps,

Shane

Sai Kumar B
Mega Sage

@Zhane 

Can you try with the below regex?

Regex - ^\S*$

find_real_file.png

 

find_real_file.png