Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For a number to be even it must end with an even digit. Even digits are 0, 2, 4, 6, and 8. Use a character class to specify which digits are allowed at each position.</p> <p>The answer is:</p> <pre><code>/^[0-9]*[02468]$/ </code></pre> <p>Explanation:</p> <pre> ^ Start of line/string [0-9] Any digit from zero to nine. * Repeat the last token zero or more times. [02468] Any even digit. $ End of line/string. </pre> <p>To help you learn regular expressions I would recommend reading the <a href="http://www.regular-expressions.info/quickstart.html" rel="noreferrer">Regular Expression Quick Start</a>.</p> <p>You may also see <code>\d</code> used instead of <code>[0-9]</code>. In some regular expression engines these are equivalent, but in others <code>\d</code> also matches characters which are regarded as numerals in foreign countries.</p> <p><strong>For more information</strong></p> <ul> <li><a href="http://www.regular-expressions.info/charclass.html" rel="noreferrer">Character classes</a></li> <li><a href="http://www.regular-expressions.info/anchors.html" rel="noreferrer">Anchors</a></li> <li><a href="http://www.regular-expressions.info/repeat.html" rel="noreferrer">Quantifiers</a></li> </ul> <p>As an exercise you could try to work out how to adjust this regular expression to disallow leading zeros. Hint: there are three types of digit: the first digit, the middle digits, the last digit. Remember that only the last digit must be present, the others are optional.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload