• Alexstarfire@lemmy.world
    link
    fedilink
    arrow-up
    7
    ·
    1 year ago

    Am I the only one shocked to learn that to find something at the end of a string it starts at the beginning? Perhaps it’s because of the simplicity of the example but I expected it to start at the end.

  • cgtjsiwy@programming.dev
    link
    fedilink
    arrow-up
    7
    arrow-down
    2
    ·
    1 year ago

    Regular expressions are great and can always be matched in linear time with respect to the input string length.

    The problem is that JS standard library RegExps aren’t actually regular expressions, but rather a much broader language, which is impossible to implement efficiently. If RegExp switched to proper regular expressions, they would match much faster but supporting backreferences like /(.*)x\1/ would be impossible.

  • sebsch@discuss.tchncs.de
    link
    fedilink
    arrow-up
    3
    arrow-down
    7
    ·
    1 year ago

    Is there one thing not screwed up in this language? I mean it’s regex, there are so many good implementations for it.

    • philnash@programming.devOP
      link
      fedilink
      arrow-up
      4
      ·
      1 year ago

      JavaScript’s regex engine isn’t the only one to have these problems. There certainly are other implementations, like Re2 and Rust’s implementation, that don’t have this issue. But they also lack some of the features of the JS implementation too.

      • sebsch@discuss.tchncs.de
        link
        fedilink
        arrow-up
        1
        arrow-down
        3
        ·
        1 year ago

        Ok thanks for the clarification.

        I would argue, the gold standard of regex would be perlre or even re from python. I never heard one discouraging using them. Do you know sth I don’t?

        • burntsushi@programming.dev
          link
          fedilink
          English
          arrow-up
          2
          ·
          1 year ago

          Both Perl and Python use backtracking regex engines and are thus susceptible to similar problems as discussed in the OP.