-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Directly return matched content instead of explicitely chopping off backticks #2
Comments
function getCodeBlocks(string)
{
const regex = /\`\`\`([a-z]*[\s\S]*?)\`\`\`/g;
var matches = regex.exec(string);
return matches == null ? [] : matches[1];
} trying this variation on your idea (accounting for no match, where |
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).
Does this make sense? |
it does make sense! although matches for me seems to look differently [ '```456```',
'456',
index: 4,
input: '123 ```456``` abc ```def```' ] |
Right, I took a better look at the result of exec (More here) Instead of Is this better? |
apparently the key,value pairs don't contribute to the length of the object, and aside from that though, it doesn't seem to capture the second match at all, like the above example should have had is 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 |
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 but it looks like |
the original code i had which used i think its probably easy (for me at least) to confuse occurrences of the regex in the string and matches in ():
but function getCodeBlocks(string)
{
const regex = /\`\`\`([a-z]*[\s\S]*?)\`\`\`/g;
const result = [];
var matches;
while((matches = regex.exec(string)) !== null) result.push(matches[1]);
return result;
} 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 |
Ohh, I get it now! Nice! |
but yeah, thanks for talking through it with me xD |
https://github.com/dmg01/snippet-bot/blob/7857918d8e390ca9a06294e5dd566626f3037161/bot.js#L112-L119
I think what you need on line 118 is something like
I'm not sure, it's a bit late here, maybe I'll test this tomorrow after some sleep :P
The text was updated successfully, but these errors were encountered: