Meta-characters are characters that have a special meaning, unlike literal characters.

What is a wildcard character?

You have learned about literal matching. Suppose you need to find instances of “bob”, “bot”, “box” in a text and replace them all with “boy”.

To solve this, you can write three RegEx and replace the result of each one with the required text.

But wait, for now, it is only three texts. Suppose you have ten texts that need replacing. Or 20! Or a 100! Writing a RegEx and replacing for each word and replacing them one by one would be incredibly tedious. In this instance, we would use a wildcard character.

Usage of wildcard character

To solve the above-mentioned problem, RegEx has a special character that matches any single character except the new line. The period, ., will match the new line while using the s single-line flag. So, instead of writing 100 RegExs, you can incorporate a special character. Now the 100 literal RegEx expressions are replaced by one special RegEx expression. And the RegEx will be /bo./.

How does a wildcard character work?

Let’s learn how this matching works. We need to find instances of “bob”, “bot”, “box”. They all have one thing in common! They all begin with literal characters, lowercase “b” and “o”. Therefore, only the last character in of all the texts changes. The first two characters will remain the same in having "b"and “o” as their prefixes.

In this case, a solution could be as follows: The character “b” is a literal match. The character “o” is a literal match. And the last character from each text will be matched by the wildcard character ".".

Now you have replaced the three RegEx with a single RegEx! Isn’t that awesome!

Get hands-on with 1400+ tech skills courses.