A regular expression is a set of characters that determines whether data entered into a field is valid. For example, it can check a phone number or email address. When you fill in a field and save your data, validation occurs. If the format is incorrect, the data won't be saved.
In this article:
What a regular expression consists of
Key elements of a regular expression.
- /…/ — The expression must be between these symbols.
- ^ — Indicates the start of a line:
/^…/
- $ — Indicates the end of a line:
/^…$/
- Characters — Letters, numbers, punctuation marks:
/^[A-Z0-9][-\s][0-9]$/
- Quantifiers — Specify how many times a character or group of characters should repeat. They go after characters like [A-Z0-9] and [0-9]:
/^[A-Z0-9]{6}[-\s][0-9]{13}$/
- Modifiers — Set options for text search or replacement. They go at the end of the regular expression after the / symbol:
/^[A-Z0-9]{6}[-\s][0-9]{13}$/g
Letters, numbers, symbols, punctuation marks.
- [0-9] — Any digit from zero to nine
- \d — Any digit; can replace [0-9]
- [a-z] — Any lowercase Latin letter
- [A-Z] — Any uppercase Latin letter
- [a-zA-Z] — Any lowercase or uppercase Latin letter
- \w — Any digit, Latin letter, or underscore
- [.,:;?!-] — Punctuation marks
- \s — A space
Quantifiers.
-
{n} — Specifies the exact number of repetitions of the preceding element. In other words, the element before {n} must repeat exactly n times. For example,
[1-3]{1}
looks for one digit between one and three. -
{min,max} — Sets a range for repetitions of the preceding element. For example, in
a{2,5}
, the letter a must appear between two and five times in a row. -
+ — The preceding character can appear one or more times. For example, in
a+
, the letter a must appear at least once but can repeat. -
? — The preceding character can appear zero or one time, making it optional. For example, in
a?
, the letter a can be absent or appear once. -
| — The OR condition. For example, the regular expression
apple|banana
searches for strings containing either "apple" or "banana." You can use the OR condition for any number of characters, likea|b|c|d
.
Modifiers. Here are the most common ones:
- i — Ignore letter case
- g — Search for all matches
- u — Support for Unicode charactersUnicode is a character encoding standard that includes symbols from nearly all world languages.
How to create a regular expression
Let's create a regular expression for a field that stores a booking code and a flight ticket number.
Define the conditions. For example:
- The field must contain a booking code and a ticket number.
- The booking code must have 6 letters or numbers.
- The ticket number must have 13 digits.
- A hyphen (-) must separate the booking code and the ticket number.
Create the expression. Use appropriate letters, symbols, punctuation marks, and modifiers.
- The booking code contains 6 letters or numbers:
[A-Z0-9]{6}
. - The ticket number contains 13 digits:
[0-9]{13}
. - A hyphen separates the code and the number:
[-]
.
The resulting regular expression is /^[A-Z0-9]{6}[-][0-9]{13}$/
.
For example, if an employee enters ABCDEF-1234567891234 in the field, they can save it.
- ABCDEF is the booking code, which contains six letters.
- A hyphen separates the booking code and the ticket number.
- 1234567891234 is the ticket number, which contains 13 digits.
The conditions of the regular expression are met, so the employee can save this value in the field.
If the value ABCDEF-123 is entered, the information won't be saved because it doesn't match the regular expression.
Examples of regular expressions
Regular expression for a phone number.
To verify if a user entered a phone number in the format XXX-XXX-XXXX, use the regular expression: /^\d{3}-\d{3}-\d{4}$/
. Here, \d
represents any digit, and the curly braces { }
specify the number of repetitions.
For example, the number 123-456-7890 matches this expression, but (123) 456 7890 does not.
Regular expression for checking a ZIP Code.
To verify if a user entered a five-digit postal code, use the regular expression: /^\d{5}$/
. Here, \d
represents any digit, and {5}
specifies that there must be five digits.
For example, the ZIP Code 65602 matches this expression, but 656002 does not.
How to add a regular expression to an SPA field
To add a regular expression:
- Go to CRM.
- Click Smart Process Automation.
- Select the SPA you need.
- Click Settings (⚙️).
- Select Field settings.
- Click the name of the field where you want to add the regular expression.
- Open the More tab.
- Add the expression in the Validation regular expression field.
- Click Save.
To check if the regular expression works correctly, enter a value and save the data.
If the data is in the correct format, Bitrix24 will save the information. If the format is incorrect, the value won't be saved.
In brief
- A regular expression is a set of characters that determines whether data entered into a field is valid.
- When you fill in a field and save your data, validation occurs. If the format is incorrect, the data won't be saved.
- Regular expressions can only be used for custom fields of the String type.
- A regular expression can consist of letters, numbers, symbols, punctuation marks, modifiers, and more.
- To add a regular expression to an SPA field, go to CRM > Smart Process Automation > Settings (⚙️) > Field settings. Select the field, open the More tab, and add the expression in the Validation regular expression field.
Recommended reading: