Regular expressions
Regular expressions (regex) are small pieces of code that enable you to match, search, validate, and otherwise manipulate text strings in an efficient, compact way. The following sections include information to help you use regex successfully in Medallia Experience Cloud.
Code basics
The following tables list common regex characters and code phrases. For a complete list, see an external regex resource, such as the reference at w3schools.com.
Character escapes
A backslash character indicates the character that follows is a special character. For example:
| Code | Description |
|---|---|
\t
| Matches a tab |
\r
| Matches a carriage return |
\n
| Matches a newline |
Character classes
Character classes match a set of characters. For example:
| Code | Description |
|---|---|
.
| Matches any single character except newline. |
\w
| Matches any word character (letters and digits). |
\W
| Matches any non-word character. |
\s
| Matches any whitespace character. |
\S
| Matches any non-whitespace character. |
\d
| Matches any decimal digit. |
\D
| Matches any character other than a decimal digit. |
[character_group]
| Matches any (case-sensitive) character in the
character_group
. For example, [abc] matches any character that is a, b, or c. |
[^character_group]
| Matches any (case-sensitive) character not in the
character_group
. |
[first-last]
| Matches any (case-sensitive) character in the
first-last
range. For example, [a-z] matches any lower-case letter in the alphabet. |
Anchors
Anchors specify positioning. For example:
| Code | Description |
|---|---|
^
| The match must start at the beginning of the text string. |
$
| The match must occur at the end of the text string. |
Quantifiers and alternations
Quantifiers specify how many instances of the previous element must be present to match. Alternations modify the code to allow either/or matching. For example:
| Code | Description |
|---|---|
*
| Matches the previous element zero or more times |
+
| Matches the previous element 1 or more times |
?
| Matches the previous element zero or 1 time |
{n}
| Matches the previous element exactly
n
times |
{n,}
| Matches the previous element at least
n
times |
{n,m}
| Matches the previous element at least
n
times but no more than
m
times |
*?
| Matches the previous element zero or more times, but as few times as possible |
+?
| Matches the previous element 1 or more times, but as few times as possible |
|
| Matches any 1 element separated by the pipe character |
Example expressions
Combine the code above to build regex phrases. For example:
| Expression | Description |
|---|---|
^[a-zA-Z]$
| Matches only letters (upper and lower case). Using ^ at the beginning and $ at the end ensures that the entire string can be only letters. |
[0-9]
| Matches only digits. Not using ^ and $ ensures that the numbers can occur anywhere in the string. |
^[0-9]{10}
| Matches a string exactly 10 digits long. |
^([a-z0-9_\.-]+)@medallia\.(?:com|eu)$
| Matches a medallia.com or medallia.eu address. |
^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$
| Matches a proper email address.
Note: Note that email address validation using regex is complex and might not work as expected in every occurrence. For example, the HTML5 standards document lists the following regular expression for the input tag when the type attribute is set to email:
|
Using regex in JavaScript
When using regex in JavaScript you must begin and end your expression with a forward slash character (for example, /expression/). You can also use modifiers, such as the following:
- Add a
gmodifier at the end of the expression to perform a global match instead of just stopping after the first match (for example,/expression/g). - Add an
imodifier at the end of the expression to perform a case-insensitive match (for example,/expression/i). - Combine these modifiers for increased effect (for example,
/expression/gi).
Common uses in Experience Cloud
You can use regex in several places when configuring Experience Cloud, including the following common cases:
- Auto Importer — Use regex for validation of receipt codes, dates, phone numbers, email addresses, and so on. For example, the following regex returns an array containing the matched receipt codes, or
nullif there is no match:var receiptCode = '123-asdfd4567-e892'; var resultArray = receiptCode.match(/\d+/g);You can also use regex to replace all occurrences of
xwithy. For example the following code replacesillywithankie, andlwithfr:var inputString = 'lily lily bo billy banana fanna fo filly me my mo milly, lily!'; var resultString = inputString.replace(/[i][l]{1,2}y/g, 'ankie'); resultString = resultString.replace(/l/g, 'fr');Restriction: Do not use/g(global) in the global<javascript-library>section of an import specification; in this case the/gis redundant and can cause performance issues.For more information about Auto Importer, see Auto Importer Preprocessors.
- K-fields — Use regex for such things as triggering an alert based on keywords, counting the number of manual resends, tracking which rapid response template was sent to the customer, and so on. For more information about K-fields, see K-fields.
- Surveys — Use regex to refine the conditions in surveys. For more information about surveys, see Surveys
- Feed pulls — Use regex for pattern matching in file names. For example:
txn_invitation_\d{8}_\d{2}.*\.txt\.ascFor more information about feed pulls, see Feed Pulls.
- Export conditions — For example, the Escape regular expression string template searches all exported data looking for a regex match. Any matches found are replaced by the Escape replacement value. For more information about export conditions, see Export formats.
Additional resources
Consider using the following external resources to learn more about using regex:
