How the following code differ and how they work?
I am learning regular expressions in javascript. I am confused about the following code. I want to know how they work, and their differences.
"abc".replace(/[a-z]??/g, "-")
// "-a-b-c-"
"abc".match(/[a-z]??/g)
// ["", "", "", ""]
"abc".replace(/[a-z]?/g, "-")
// "----"
"abc".match(/[a-z]?/g);
// ["a", "b", "c", ""]
Why there are four matches for both of them? How come the first replace method insert the -
at both beginning and end?
Why the second only match the end not the beginning of the "abc"?