Using Regular Expressions (Regex) With Your App

This article provides valuable resources and helpful tips for utilizing Regex effectively.

What are Regular Expressions?

Regular Expressions, also called Regex, are strings of characters that allow you to define specific search patterns.

Using Regular Expressions with Your App

Regular expressions can be used within validation rules and text formulas to perform many functions. For more information on using Regex within your Knack app, take a look at the following articles:

Getting Help With Regular Expressions

Note: Unfortunately, we cannot provide support for Regex as it falls outside our scope of assistance. This means that we cannot offer any further help with setup or troubleshooting.

Additionally, since Regex is a code-based solution, we cannot guarantee its functionality or full compatibility with Knack.

 

While we can’t help further, many resources can point you in the right direction:

  • You can ask for help in our community of other Knack users.

  • You can learn about, write, and test regex at Regex 101. Use the ECMAScript/Javascript option.

  • Rubular is a helpful regex editor to test regular expressions as you write them.

  • Share your question in external developer communities such as Stack Overflow.

Regular Expression FAQs

Can I prevent users from submitting blank or only spaces on form fields?

Yes, you can prevent whitespace characters from being submitted in fields using regular expressions (regex) in field validation rules

To do this, you'll need to create a validation rule in the field(s) involved. This should look like when [field] does not match regular expression ^(?=.*\S).+$ followed by your error message.

Can I extract the username from an email address?

Yes, you can use the extractRegex function in a Text Formula field to extract the username before the "@" in an email address using the following:

extractRegex({Email_Field},([^@]+) 

Can I validate a URL entered into a Link field type?

Yes, you can validate a user-entered URL in a link field type by utilizing two fields for the validation:

1. A short text field type where the user inputs the value, and this field will have a robust validation rule that employs regex.

2. A link field type which allows the URL to be clicked and accessed.

Therefore, you will need to create two fields in total:

The Short Text field where the user inputs the URL value will contain a regex validation rule that looks like this:

^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$
Note: You might need to adjust the regex to suit your specific needs. Please note that this is not a specific recommendation.
 

Next, you'll apply a conditional rule to the Link field to set its value using the URL captured in the Short Text field. The Link field is needed if you want users to be able to click to access the URL: