Regular expression difference between * and ? to match elements that may exist in a string


When do you use * and when ? to look for elements that may exist in a string?

let str = favourite
let regex1 = /favou*rite/;
let regex2 = /favou?rite/;

regex1.test(str) // returns true
regex2.test(str) // returns true

Do they mean the same thing? I don’t understand how they are different


The ? means “0 or 1 times”, the * means “0 or more times”

so if you have a string like "favouurite" (remember to wrap your strings in quotes!)

let fav = "favouurite";

regex1.test(fav);  // returns true
regex2.test(fav);  // returns false
1 Like

Of course, had not noticed that. thank you.