Question: I saw this weird regex in javascript.

```
string = "abababababababa";
string=string.replace(/ ?a ?/g,"X");
alert(string);  

```

I ran it , and the output I got was all a replaced by X
What is puzzling is the white spaces in the regex. I delete the first white space, the script won't run. I delete the 2nd white space, I get one "a" replaced by two "X"s.
I wonder how it works.

Answer: 
The space actually means to match a space character (U+0020).

The key is the [`?` quantifier](http://www.regular-expressions.info/optional.html) that follows each of them, allowing the pattern to match "*0 or 1 spaces*" for each, essentially making them *optional*.

So, the `/ ?a ?/` pattern is capable of matching:

* `"a"`
* `"a "`
* `" a"`
* `" a "`

And, attempting to remove either space will change the meaning of the pattern:

* Removing the leading space (`/?a ?/g`) actually results in a `SyntaxError` as quantifiers require something before them to quantify.
* Removing the trailing space (`/ ?a?/g`) is syntactically valid, but the `?` quantifier will then apply to the `a`, changing the possible matches to:

	+ `""`
	+ `"a"`
	+ `" "`
	+ `" a"`
the `?` character makes the spaces optional in matching.

For example, these strings will be replaced with an X with that regular expression.

```
> "a"
> " a"
> " a "
> "a "

```

You can find more information about this quantifier **[here](http://www.regular-expressions.info/optional.html)**.