!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).



!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
\1will correctly match the first capture group which ist,\2will match the second capture group which isr, and so on so forth… Oh, and it works beyond.replacecontexts, during.matchas 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.