I saw this weird regular expression in javascript.
string = "abababababababa";
string=string.replace(/ ?a ?/g,"X");
alert(string);
I ran it, and the result that I got was replaced with X. What causes bewilderment are spaces in the regular expression. I delete the first empty space, the script will not work. I delete the second empty space, I get one "a" replaced by two "X". I wonder how it works.
source
share