最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - How do you read this JavaScript regular expression? - Stack Overflow

programmeradmin4浏览0评论
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.

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.

Share Improve this question edited Oct 8, 2019 at 23:38 Wiktor Stribiżew 627k41 gold badges496 silver badges611 bronze badges asked Nov 9, 2008 at 5:04 GR1000GR1000 2
  • 1 Re: update 2. Way overthinking. The expression merely says "match the beginning of the string, followed by 1 or more 0s, followed by the end of the string". Don't think of ^ and $ as locations, but as matchers for the (nonexistent) beginning char and terminator. – billjamesdev Commented Nov 11, 2008 at 0:54
  • Maybe ... but asking this question made me realize it's not enough to simply know the definition -- I need to "get under the hood" and learn the REASON for the definition -- it's the algorithm I'm after! :) – GR1000 Commented Nov 11, 2008 at 23:02
Add a comment  | 

8 Answers 8

Reset to default 10

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

^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) «$»

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.

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

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+)$/

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

/(^0+|0+$)/

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.

发布评论

评论列表(0)

  1. 暂无评论