Regular expressions are greedy by default, meaning they try to match as many strings as possible. One of the ways to write accurate regular expressions is to make them as lazy as possible.
The metacharacter that helps achieve laziness is the question mark
?. It helps you make any RegEx pattern optional.
In this article, we’ll take a look at the question mark metacharacter and how you can make any RegEx pattern optional with it.
What is the Question Mark
The question mark
? metacharacter is a quantifier that specifies 0 or 1. The other common quantifiers are asterisk (
*) and plus (
+). Asterisk means 0 or many and plus means 1 or many.
Unlike many other metacharacters, you don’t need to escape the question mark
? metacharacter to make it work. The only time you need to escape it is when you want to match the question mark symbol itself.
How to Use the Zero or One
? Quantifier in RegEx
The question mark quantifier can be useful when you are expecting two different but acceptable inputs from a user.
For instance, if you expect the user to type in “color” or “behavior”, but you’re not sure whether they speak British or American English, you can make the "u" optional:
By putting the question mark symbol in front of the letter “u”, it means the "u" is optional. So, "color" and "colour" get matched, "behavior" and "behaviour" also get matched.
You can also use the question mark quantifier to reduce greediness in your regular expressions.
For example, let’s try to match everything inside the paragraph tags below:
<p>freeCodeCamp is the best place to learnn coding for free</p> <p>freeCodeCamp is ubiquitous</p> <p>You can learn any tech skill with freeCodeCamp</p>
/<p>.*<\/p>/g would match everything inside the tags. Everything would be two matches due to the carriage return separating the last
p tag from the rest:
You can separate all the tags into separate matches with the zero or one quantifier (
And if you want just the first paragraph tag to be the match, you can turn off the
The question zero or one (
let str1 = 'Color is American'; let str2 = 'Colour is British'; let re1 = /colou?r/i; console.log(re1.exec(str1)); // [ 'Color', index: 0, input: 'Color is American', groups: undefined ] console.log(re1.exec(str2)); // [ 'Colour', index: 0, input: 'Colour is British', groups: undefined ]
The zero or one quantifier helps stop regular expressions from being greedy by making a string optional on its own or within others. You should use it whn necessary in order to write less greedy and more accurate regular expressions.