Loading ...
Sorry, an error occurred while loading the content.

Re: Disambiguating the slash operator?

Expand Messages
  • Douglas Crockford
    ... This case is decidable, which is why JavaScript parsers are able to work, but there are problems in the language because it can be difficult to determine
    Message 1 of 3 , Jan 8, 2009
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "crlender" <crlender@...> wrote:

      > This function:
      >
      > function isValidBZ (val) {
      > return /^(?:[1-9]\d*|0)$/.test(val);
      > }
      >
      > triggers the JSLint warning "Wrap the /regexp/ literal in parens
      > to disambiguate the slash operator". Could you clarify this? I
      > can't see any ambiguities in that line.

      This case is decidable, which is why JavaScript parsers are able to
      work, but there are problems in the language because it can be
      difficult to determine the meaning of / without doing a full parse.
      That is why I recommend that you write

      return (/^(?:[1-9]\d*|0)$/).test(val);
    • crlender
      ... Thanks for the reply. I ve never seen an example of where the slash in regex literals could be ambiguous for either humans or the parser. As far as I know,
      Message 2 of 3 , Jan 10, 2009
      • 0 Attachment
        "Douglas Crockford" <douglas@...> wrote:
        > > return /^(?:[1-9]\d*|0)$/.test(val);
        ...
        > This case is decidable, which is why JavaScript parsers are able to
        > work, but there are problems in the language because it can be
        > difficult to determine the meaning of / without doing a full parse.
        > That is why I recommend that you write
        >
        > return (/^(?:[1-9]\d*|0)$/).test(val);

        Thanks for the reply. I've never seen an example of where the slash
        in regex literals could be ambiguous for either humans or the parser.
        As far as I know, the parser always does a "full parse" in any case,
        and the ECMAScript syntax doesn't contain any ambiguous passages that
        I'm aware of. The same isn't always true for humans "parsing" code
        (typical gotchas are automatic semicolon insertion, and assignments
        where comparisons would be expected), so this JSLint message could
        indeed be valuable for debugging. I've just never seen a case where
        the slash was problematic.
        I would be grateful if you or another subscriber on this list could
        provide an example of such an ambiguous use of a regex literal.

        Thanks,

        - Conrad
      Your message has been successfully submitted and would be delivered to recipients shortly.