Regular Expressions: Wildcard
Welcome to the Wildcard lesson!
This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!
Regexes like /a/ are literal: they specify exact characters to match. The real power in regexes is in the various operators. The most basic is
., the wildcard operator. It matches any character. But the character must be present;.won't match the empty string.>
/./.test('a');Result:
true
>
/./.test('b');Result:
true
>
/./.test('>');Result:
true
>
/./.test('');Result:
false
There's one important special case for
/./: it doesn't match newlines!>
/./.test('\n');Result:
false
Putting
.next to another character means that they occur consecutively. For example,/a./matches an "a" followed by any character. And/.a/matches any character followed by an "a".>
/x.z/.test('xyz');Result:
true
>
/x.z/.test('xaz');Result:
true
>
/x.z/.test('xyyz');Result:
false
>
/x.z/.test('x_z');Result:
true
When there are multiple
.s, they can match different characters.>
/x..z/.test('xaaz');Result:
true
>
/x..z/.test('xaz');Result:
false
>
/x..z/.test('xabz');Result:
true