Topics Map > D2L/Brightspace > Quizzes and Surveys
D2L Quizzes and Surveys  About Regular expressions in quiz questions
Learn about regular expressions and how to use them in quiz questions in the Question Library.
What are regular expressions?
A regular expression is a pattern of text that is interpreted for special meaning. The pattern describes one or more strings to match when searching a body of text. The regular expression serves as a template.
Regular expressions consists of ordinary characters that match themselves literally (letters az and AZ, digits 09, the underscore, and a few other characters) and characters similar to wild card characters, known as metacharacters.
Regular expressions in quiz questions
It is often convenient to use a regular expression to include multiple possible answers to a quiz question instead of specifying a separate answer for each possibility. For example, if we want to allow the answers "gray" and "grey" to the question "What color paint do we get when we mix black paint with white paint?", we could use the regular expression
gr[ae]y
where the [ae] part matches either a or e. Both gray and grey would be flagged as correct answers.
Using regular expressions
Regular expressions can be used in Short Answer, Fill In the Blank, and MultiShort Answer questions. They can also be used for Units in calculation type questions, i.e., Arithmetic and Significant Figures questions.
To use regular expressions in possible answers for Short Answer, Fill In the Blank, or MultiShort Answer questions, select the Regular Expression option under "Evaluation" for each answer that you want to use a regular expression:
Short Answer Example
Fill In the Blank Example
MultiShort Answer Example
To use regular expressions in Units for calculation type questions, select the Regular Expression option under "Units".
Units Example
Caution: consider formatting
Case sensitivity
When regular expressions are used, answer matching is casesensitive. For example, if the regular expression in the answer box contains [Hh]ot dog, then the answers Hot dog and hot dog will be flagged correct but HOT DOG and Hot Dog will be flagged incorrect.
If regular expressions are not used, answer matching is casesensitive if the Case Sensitive option is selected and not casesensitive if the Case Insensitive option is selected.
If the answer box contains hot dog and the Case Sensitive option is selected, Hot dog, Hot Dog, and HOT DOG will be flagged incorrect. If the Case Insensitive option is selected, all of the above as well as HoT dOg and other combinations of lowercase and uppercase letters will be flagged correct.
Extra spaces
White space is disregarded in an answer. In the above example, we could have two or more, instead of one, space between hot and dog and the answer would be flagged correct. We could also have space(s) before hot or after dog and the answer would be flagged correct.
Common metacharacters
Metacharacter 
Description 
Example 

\  Marks the next character as a special character, a literal, a backreference, or an octal escape. 

^  Matches the position at the beginning of the input string. If the RegExp object’s Multiline property is set, ^ also matches the position following '\n' or '\r'. 

$  Matches the position at the end of the input string. If the RegExp object’s Multiline property is set, $ also matches the position preceding '\n' or '\r'. 

*  Matches the preceding character or subexpression zero or more times.


+  Matches the preceding character or subexpression one or more times.


?  Matches the preceding character or subexpression zero or one time.


When this character immediately follows any of the other quantifiers (*, +, ?, {n}, {n,}, {n,m}), the matching pattern is nongreedy. A nongreedy pattern matches as little of the searched string as possible, whereas the default greedy pattern matches as much of the searched string as possible. 


()  Parentheses create a substring or item that you can apply metacharacters to. 

{n,}  n is a nonnegative integer. Matches exactly n times. 

{n}  n is a nonnegative integer. Matches at least n times. 

{n,m}  m and n are nonnegative integers, where n <= m. Matches at least n and at most m times. Note: You cannot put a space between the comma and the numbers. 

.  Matches any single character except "\n". To match any character including the '\n', use a pattern such as '[\s\S]'. 

(?!)  Makes the remainder of the regular expression case insensitive. 

(pattern)  Matches pattern and captures the match.


(?:pattern)  Matches pattern but does not capture the match, that is, it is a noncapturing match that is not stored for possible later use. This is useful for combining parts of a pattern with the "or" character (). 

(?=pattern)  Positive lookahead matches the search string at any point where a string matching pattern begins. This is a noncapturing match, that is, the match is not captured for possible later use.


(?!pattern)  Negative lookahead matches the search string at any point where a string not matching pattern begins. This is a noncapturing match, that is, the match is not captured for possible later use.


xy  Matches x or y. 

[xyz]  A character set. Matches any one of the enclosed characters. 

[^xyz]  A negative character set. Matches any character not enclosed. 

[az]  A range of characters. Matches any character in the specified range. 

[^az]  A negative range of characters. Matches any character not in the specified range. 

\b  Matches a word boundary: the position between a word and a space. 

\B  Matches a nonword boundary. 

\cx  Matches the control character indicated by x. The value of x must be in the range of AZ or az. If not, c is assumed to be a literal 'c' character. 

\d  Matches a digit character. Equivalent to [09]  
\D  Matches a nondigit character Equivalent to [^09]  
\f  Matches a formfeed character. Equivalent to \x0c and \cL  
\n  Matches a newline character. Equivalent to \x0a and \cJ  
\r  Matches a carriage return character. Equivalent to \x0d and \cM  
\s  Matches any white space character including space, tab, formfeed, etc. Equivalent to [\f\n\r\t\v] 

\S  Matches any nonwhite space character. Equivalent to [^\f\n\r\t\v]  
\t  Matches a tab character. Equivalent to \x09 and \cI  
\v  Matches a vertical tab character. Equivalent to \x0b and \cK  
\w  Matches any word character including underscore. Equivalent to '[AZaz09_]'  
\W  Matches any nonword character. Equivalent to '[^AZaz09_]'. You should only use \D, \W and \S outside character classes.  
\Z  Matches the end of the string the regular expression is applied to. Matches a position, but never matches before line breaks. 

\xn  Matches n, where n is a hexadecimal escape value. Hexadecimal escape values must be exactly two digits long. Allows ASCII codes to be used in regular expressions. 

\num  Matches num, where num is a positive integer. A reference back to captured matches. 

\n  Identifies either an octal escape value or a backreference. If \n is preceded by at least n captured subexpressions, n is a backreference. Otherwise, n is an octal escape value if n is an octal digit (07). 

\nm  Identifies either an octal escape value or a backreference. If \nm is preceded by at least nm captured subexpressions, nm is a backreference. If \nm is preceded by at least n captures, n is a backreference followed by literal m. If neither of the preceding conditions exists, \nm matches octal escape value nm when n and m are octal digits (07).  
\nml  Matches octal escape value nml when n is an octal digit (03) and m and l are octal digits (07).  
\un  Matches n, where n is a Unicode character expressed as four hexadecimal digits. 
