Skip to content

Commit 43e1151

Browse files
ScratchFakemonDangoCatSharkPool-SP
authored
text.js -- Add array of regex matches block (#2306)
Added modified versions of `split` and `matchRegex` that both return an array instead of a specific item, which can be useful in more complex projects. I also moved `testRegex` so all of the match blocks could be together in the palette. Pretty quick, so there isn't that much polish. It was mostly an easy copy-and-paste job and things are still left in. --------- Co-authored-by: DangoCat[bot] <[email protected]> Co-authored-by: SharkPool <[email protected]>
1 parent 635bd7a commit 43e1151

File tree

1 file changed

+60
-4
lines changed

1 file changed

+60
-4
lines changed

extensions/text.js

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,11 @@
296296
},
297297
},
298298
{
299-
opcode: "countRegex",
299+
opcode: "matchRegexJSON",
300300
blockType: Scratch.BlockType.REPORTER,
301301
text: Scratch.translate({
302-
default: "count regex /[REGEX]/[FLAGS] in [STRING]",
302+
default:
303+
"matches of [STRING] using regex /[REGEX]/[FLAGS] as array",
303304
description:
304305
"/[REGEX]/ is supposed to match the syntax that some actual programming languages used for regular expressions.",
305306
}),
@@ -310,11 +311,11 @@
310311
},
311312
REGEX: {
312313
type: Scratch.ArgumentType.STRING,
313-
defaultValue: "[AEIOU]",
314+
defaultValue: "(.) (.{2})",
314315
},
315316
FLAGS: {
316317
type: Scratch.ArgumentType.STRING,
317-
defaultValue: "i",
318+
defaultValue: "g",
318319
},
319320
},
320321
},
@@ -341,6 +342,29 @@
341342
},
342343
},
343344
},
345+
{
346+
opcode: "countRegex",
347+
blockType: Scratch.BlockType.REPORTER,
348+
text: Scratch.translate({
349+
default: "count regex /[REGEX]/[FLAGS] in [STRING]",
350+
description:
351+
"/[REGEX]/ is supposed to match the syntax that some actual programming languages used for regular expressions.",
352+
}),
353+
arguments: {
354+
STRING: {
355+
type: Scratch.ArgumentType.STRING,
356+
defaultValue: "Hello world!",
357+
},
358+
REGEX: {
359+
type: Scratch.ArgumentType.STRING,
360+
defaultValue: "[AEIOU]",
361+
},
362+
FLAGS: {
363+
type: Scratch.ArgumentType.STRING,
364+
defaultValue: "i",
365+
},
366+
},
367+
},
344368

345369
"---",
346370

@@ -637,6 +661,38 @@
637661
return "";
638662
}
639663
}
664+
matchRegexJSON(args, util) {
665+
// matchRegex but it returns an array
666+
try {
667+
const string = Scratch.Cast.toString(args.STRING);
668+
const uncleanRegex = Scratch.Cast.toString(args.REGEX);
669+
const flags = Scratch.Cast.toString(args.FLAGS);
670+
671+
// Cache the last matched string
672+
if (
673+
!(
674+
matchCache &&
675+
matchCache.string === string &&
676+
matchCache.regex === uncleanRegex &&
677+
matchCache.flags === flags
678+
)
679+
) {
680+
const newFlags = flags.includes("g") ? flags : flags + "g";
681+
const regex = new RegExp(uncleanRegex, newFlags);
682+
683+
matchCache = {
684+
string,
685+
regex: uncleanRegex,
686+
flags,
687+
arr: string.match(regex) || [],
688+
};
689+
}
690+
return JSON.stringify(matchCache.arr) || "[]";
691+
} catch (e) {
692+
console.error(e);
693+
return "";
694+
}
695+
}
640696

641697
countRegex(args, util) {
642698
// Fill cache

0 commit comments

Comments
 (0)