views:

594

answers:

8
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.

+9  A: 

It's saying your string contains only 0... it must begin, then have 1 or more 0's, then end.

Bill James
+7  A: 

^0+$

  • Assert position at the beginning of the string «^»
  • Match the character “0” literally «0+»
    • Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
  • Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
CMS
+2  A: 

If you want it to match your string, you want your regexp to be something like:

var pattern = /^0+.*0+$/

This will match 1 or more '0's at the beginning, 1 or more '0's at the end, and 0 or more of anything in the middle.

drhorrible
Think he wanted "starts with 0s OR ends with 0s"... yours requires a 0 at start and end.
Bill James
No, he wanted "one or more zeros at the beginning and the end"
drhorrible
+2  A: 

A pattern of at least one 0, anchored to both the beginning (^) and end ($) of a line; in other words, the string consists of one or more zeroes.

To match a string consisting of digits 0 to 9, and starting and ending with at least one zero, the pattern

/^0+[0-9]+0$/

would work, i.e. match one or more zeroes, then one or more digits 0-9, ending in a zero.

Unless otherwise instructed, JavaScript will treat the entire string as one line, ignoring newline characters; to force it to consider multiple separate lines, use the m modifier, e.g.

/regex/m

See also: JavaScript regular expression overview on Mozilla Developer Center

Rob
A: 
/(^0+|0+$)/
eyelidlessness
+1  A: 

Responding to your update -- you're kind of right. The string the regex will try to match does have a beginning, a middle and an end.

But you know what? All strings have a beginning and an end.

Your regex says, the string must have a beginning, an end, and in between the two, only a string of zeros.

Don't think of your 000000 as being at the start, or your 900000 as being at the end. In fact, ignore the idea of beginning and end for a moment.

  • This regex matches "must have some zeros in it somewhere": /0+/
  • This regex matches "must have some zeros in it and nothing else: /^0+$/

why do we add the ^ and the $?

Because there's no regex notation for "and nothing else", so we have to specify "start of string, some zeros, end of string", which comes to the same thing.

By the way, you got this wrong too:

if there's a pattern of one or more zeros at the beginning and the end, then return that pattern

your regex won't return "that pattern", it will only return true. You need brackets to get hold of the pattern: /^(0+)$/

AmbroseChapel
A: 

I always put my Regex's into human words to make sure it's what I meant.

This: ^0+$

Would read as follows: "If from the start (^) of my string, you find at least one (+) zero (0), and nothing else until the end ($) of the string."

Remember, a regex is basically a bunch of "If" statements. So first, it has to be "true", and THEN it will return the best possible result. A Regex won't ever return a "somewhat-true", or a "mostly-true" result.

Timothy Khouri
A: 

@CodeCurious, you're still making it more complicated than it needs to be. The regex /^0+$/ merely means the entire string consists of one or more zeros. Regexes offer plenty of opportunities for confusion, so accept simplicity wherever you can find it.

Alan Moore
Believe me, this isn't the first time I've been told I make things more complicated than necessary! :) But, seriously, I realize at face value, this regex is pretty simple. But my brain needs to see it from a deeper level in order to truly understand it.