Mark As Completed Discussion

This code snippet prints "Found a match!" as a single-digit value (in the range of 0 to 9) is present in the string. We can combine these square brackets to obtain more interesting results (such as matching consecutive characters) as illustrated below.

Special Characters Part 2

[a-z] matches any character between 'a' and 'z', [0-9] matches any character between '0' and '9'. Since they are placed right next to each other, the match is found consecutively.

Special Characters Part 2

Here the third part of the expression, [a-z] did not match the third consecutive character of the pattern. As a result, a matching string was not found.

Characters and digits can also be combined for matching, such as in the following example.

Special Characters Part 2

Let's see another metacharacter, a period (.). A period matches any single character occurring at that specific place in a string (except newline character).

The regex 'chips.dip' matches any string which has any single character in-between 'chips' and 'dip', such as the one given in input 'chipsndip' or 'chipsmdip'.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment