var pattern = /^0+$/;
My guess is this:
"Take a look at both the beginning and the end of the string, and if there's a pattern of one or more zeros at the beginning and the end, then return that pattern."
I'm sure that's wrong, though, because when I run the expression with this string:
var string = "0000009000000";
It comes up null
.
So what's it really saying? And while I'm asking, what/how does JavaScript consider the beginning, middle and end of a string?
UPDATE #1: Thanks for the responses! I think I understand this now. My confusion stemmed from the fact that I'm visualizing the string as having a beginning, a middle and an end. Like this:
[beginning][middle][end]
In other words, for the given string above, the following expressions work as I expect them to:
/^0+/;
returns "000000" (a pattern of one or more zeros at the beginning of the string)
and
/0+$/
; returns "000000" (a pattern of one or more zeros at the end of the string)
UPDATE #2: I up-voted all the responses to this point, because they're all helpful, and I compiled the answers into one great big one:
Given the following JavaScript code:
var string = "0000009000000";
var regExp = /^0+$/;
alert(regExp.exec(string));
It reads, in part, like this:
"If the exact character(s) followed by the ^ modifier and preceded by the $ modifier in the regular expression are not SIMULTANEOUSLY sitting in the first position(s) of the string AND the last position(s) of the string (i.e., they are not the only character(s) in the string), then return null
. Else, return the character(s)."
In other words, let's say the given string is six zeros "000000". This results in a match because the exact same group of "0" characters are sitting in BOTH the first positions (1st 2nd 3rd 4th 5th 6th) AND the last positions (1st 2nd 3rd 4th 5th 6th) of the string.
However, in the original given string, there are six zeros, followed by a nine, followed by six zeros ("0000009000000"). Now, the six zeros in the first positions of the string (1st, 2nd, 3rd, 4th, 5th, 6th) are NOT the exact same six zeros sitting in the last positions of the string (8th, 9th, 10th, 11th, 12th, 13th). Hence, a null
is returned.