Open
Description
https://github.com/dmg01/snippet-bot/blob/7857918d8e390ca9a06294e5dd566626f3037161/bot.js#L112-L119
I think what you need on line 118 is something like
var matches = regex.exec(string)
return (matches[1] || [])
I'm not sure, it's a bit late here, maybe I'll test this tomorrow after some sleep :P
Activity
MegaLoler commentedon Nov 27, 2017
trying this variation on your idea (accounting for no match, where
matches
is set tonull
), it works for me, but it only extracts the first match, so if there are multiple code blocks, it ignores all but the first hahadennib commentedon Nov 28, 2017
Oh I see you're right, I explicitly indicated matches[1] as value to return (that is only the first match), I misread the behavior of the function.
Anyway, given n matches you should have an array (matches) of length n + 1 where matches[0] is the entire string (I think).
So in case we have at least 1 match, we could do something with matches array cutting off the first element, so something like:
return matches == null ? [] : matches.shift();
Does this make sense?
MegaLoler commentedon Nov 28, 2017
it does make sense! although matches for me seems to look differently
given the input string
"123 ```456``` abc ```def```"
, the result ofregex.exec
is this:dennib commentedon Nov 28, 2017
Right, I took a better look at the result of exec (More here)
It seems like we have to chop off not only the first array element but also the last two (index and input properties), so i think we could try something like this:
Instead of
return matches == null ? [] : matches.shift();
somenthing like
return matches == null ? [] : matches.slice(1,matches.length - 2);
Is this better?
MegaLoler commentedon Nov 28, 2017
apparently the key,value pairs don't contribute to the length of the object, and
matches.length
is actually just 2 xD (so my result from your suggestion there is always an empty array)aside from that though, it doesn't seem to capture the second match at all, like the above example should have had
'def'
included in the matches as well, which is weird, because the docs totally say it should be an array of all the matchesis something amiss with the regex? is there perhaps a regex flag we need to make it process all of them? (although i thought that's what
g
was supposed to do)MegaLoler commentedon Nov 28, 2017
oh, i think i get it better, matches looks like its supposed to return an array corresponding to different groups in the regex, so since we only have one pair of
()
there's only one matchbut it looks like
regex.exec
can be called iteratively to get successive matches, i'll try that out!MegaLoler commentedon Nov 28, 2017
the original code i had which used
match
captured all the occurrences successfully, but it did have the backticks in the resulti think its probably easy (for me at least) to confuse occurrences of the regex in the string and matches in ():
exec
seems to return an array of matches in () (so we only get 1 match because we only have one pair of()
in our regex)match
seems to return an array of occurrences of the regex in the stringbut
exec
can also iteratively return results for each occurrence of the regex when called iteratively on the sameRegEx
object, which i got working like this:which feels a little out of the way to me personally, but maybe i'm just trying to overly minimize the code xD it works fine anyways
i wonder which one is ultimately nicer, the original code with
match
and slicing off backticks, or this iterative one withexec
dennib commentedon Nov 28, 2017
Ohh, I get it now! Nice!
Seems strange to me too, but I'm glad it works now anyway :P
MegaLoler commentedon Nov 28, 2017
but yeah, thanks for talking through it with me xD