If I were you, I’d knee before the Great Owl. Who?, you may ask. Exactly! Who!

  • 0 Posts
  • 2 Comments
Joined 1 month ago
cake
Cake day: May 12th, 2026

help-circle
  • !onehundredninetysix@lemmy.blahaj.zone
    There’s something else: the backslash followed by a positive natural number means a reference to the nth capture group, so:

    "truthfultemporarily".match(/(t)(r)(u)\1hf\3(l)\1empo\2a\2i\4y/)

    …as esoteric as it may sound, will match your Lemmy username, because the \1 will correctly match the first capture group which is t, \2 will match the second capture group which is r, and so on so forth… Oh, and it works beyond .replace contexts, during .match as well.

    Source: I just learned through this very meme and, from now on, I’ll likely use this feature whenever I have to use RegExp because I love coding cryptic one-liners just for the sake of it.

    Screenshot of DevTools illustrating the working of the aforementioned snippet, with its output correctly matching the string.


  • !onehundredninetysix@lemmy.blahaj.zone TIL, through a meme (yep, memes can be very teaching, too), that I can reuse capture groups in a recurring manner inside a RegExp (I didn’t know about the \(number) thing, but I readily inferred, due to past experience with using \(number) in KDE Kate’s Regexp replace, it had something to do with “this position must contain the nth group verbatim”, opened the DevTools, tried .match with a fixed version of the meme’s regex (i.e. without the invalid spaces) and a random phone sequence my mind conjured out of thin air, and voilá, the slash-number thing indeed behaved as I guessed it would behave). So… Thanks to whoever made the meme because TIL thanks to you!

    /^Be( )not\1(a)fr\2id$/ (Biblically-accurate RegExp).