From 08195d601ba711423f141ed8a623dab0cd8ad784 Mon Sep 17 00:00:00 2001 From: Scratch_Fakemon Date: Wed, 22 Oct 2025 14:18:02 -0500 Subject: [PATCH 1/6] Add splitJSON and matchRegexJSON Added modified versions of split and matchRegex that return an array instead of a specific item, which can be useful in more complex projects. --- extensions/text.js | 103 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 4 deletions(-) diff --git a/extensions/text.js b/extensions/text.js index dd77ac882a..b787dfe5df 100644 --- a/extensions/text.js +++ b/extensions/text.js @@ -139,6 +139,21 @@ }, }, }, + { + opcode: "splitJSON", + blockType: Scratch.BlockType.REPORTER, + text: Scratch.translate("[STRING] split by [SPLIT] as array"), + arguments: { + STRING: { + type: Scratch.ArgumentType.STRING, + defaultValue: "apple", + }, + SPLIT: { + type: Scratch.ArgumentType.STRING, + defaultValue: "p", + }, + }, + }, { opcode: "count", blockType: Scratch.BlockType.REPORTER, @@ -296,10 +311,11 @@ }, }, { - opcode: "countRegex", + opcode: "matchRegexJSON", blockType: Scratch.BlockType.REPORTER, text: Scratch.translate({ - default: "count regex /[REGEX]/[FLAGS] in [STRING]", + default: + "[STRING] matched by regex /[REGEX]/[FLAGS] as array", description: "/[REGEX]/ is supposed to match the syntax that some actual programming languages used for regular expressions.", }), @@ -310,11 +326,11 @@ }, REGEX: { type: Scratch.ArgumentType.STRING, - defaultValue: "[AEIOU]", + defaultValue: "(.) (.{2})", }, FLAGS: { type: Scratch.ArgumentType.STRING, - defaultValue: "i", + defaultValue: "g", }, }, }, @@ -341,6 +357,30 @@ }, }, }, + { + opcode: "countRegex", + blockType: Scratch.BlockType.REPORTER, + text: Scratch.translate({ + default: "count regex /[REGEX]/[FLAGS] in [STRING]", + description: + "/[REGEX]/ is supposed to match the syntax that some actual programming languages used for regular expressions.", + }), + arguments: { + STRING: { + type: Scratch.ArgumentType.STRING, + defaultValue: "Hello world!", + }, + REGEX: { + type: Scratch.ArgumentType.STRING, + defaultValue: "[AEIOU]", + }, + FLAGS: { + type: Scratch.ArgumentType.STRING, + defaultValue: "i", + }, + }, + }, + "---", @@ -547,6 +587,29 @@ } return splitCache.arr[item - 1] || ""; } + splitJSON(args, util) { // Split, but it returns an array + const string = Scratch.Cast.toString(args.STRING); + const split = Scratch.Cast.toString(args.SPLIT); + const item = Scratch.Cast.toNumber(args.ITEM); + + // Cache the last split + if ( + !( + splitCache && + splitCache.string === string && + splitCache.split === split + ) + ) { + const regex = this._caseInsensitiveRegex(split); + + splitCache = { + string, + split, + arr: string.split(regex), + }; + } + return JSON.stringify(splitCache.arr) || "[]"; + } count(args, util) { // Fill cache @@ -637,6 +700,38 @@ return ""; } } + matchRegexJSON(args, util) { // matchRegex but it returns an array + try { + const string = Scratch.Cast.toString(args.STRING); + const uncleanRegex = Scratch.Cast.toString(args.REGEX); + const flags = Scratch.Cast.toString(args.FLAGS); + const item = Scratch.Cast.toNumber(args.ITEM); + + // Cache the last matched string + if ( + !( + matchCache && + matchCache.string === string && + matchCache.regex === uncleanRegex && + matchCache.flags === flags + ) + ) { + const newFlags = flags.includes("g") ? flags : flags + "g"; + const regex = new RegExp(uncleanRegex, newFlags); + + matchCache = { + string, + regex: uncleanRegex, + flags, + arr: string.match(regex) || [], + }; + } + return JSON.stringify(matchCache.arr) || "[]"; + } catch (e) { + console.error(e); + return ""; + } + } countRegex(args, util) { // Fill cache From 3da46314ea8e8d41d35f5dd5b2cf957d5383c0f5 Mon Sep 17 00:00:00 2001 From: Scratch_Fakemon Date: Wed, 22 Oct 2025 14:49:33 -0500 Subject: [PATCH 2/6] Make the linter happy --- extensions/text.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/extensions/text.js b/extensions/text.js index b787dfe5df..210bbad90e 100644 --- a/extensions/text.js +++ b/extensions/text.js @@ -314,8 +314,7 @@ opcode: "matchRegexJSON", blockType: Scratch.BlockType.REPORTER, text: Scratch.translate({ - default: - "[STRING] matched by regex /[REGEX]/[FLAGS] as array", + default: "[STRING] matched by regex /[REGEX]/[FLAGS] as array", description: "/[REGEX]/ is supposed to match the syntax that some actual programming languages used for regular expressions.", }), @@ -380,7 +379,6 @@ }, }, }, - "---", @@ -587,10 +585,10 @@ } return splitCache.arr[item - 1] || ""; } - splitJSON(args, util) { // Split, but it returns an array + splitJSON(args, util) { + // split but it returns an array const string = Scratch.Cast.toString(args.STRING); const split = Scratch.Cast.toString(args.SPLIT); - const item = Scratch.Cast.toNumber(args.ITEM); // Cache the last split if ( @@ -700,12 +698,12 @@ return ""; } } - matchRegexJSON(args, util) { // matchRegex but it returns an array + matchRegexJSON(args, util) { + // matchRegex but it returns an array try { const string = Scratch.Cast.toString(args.STRING); const uncleanRegex = Scratch.Cast.toString(args.REGEX); const flags = Scratch.Cast.toString(args.FLAGS); - const item = Scratch.Cast.toNumber(args.ITEM); // Cache the last matched string if ( From f49c9a81ab0fe6f8457bd44b719791f95aa7df33 Mon Sep 17 00:00:00 2001 From: Scratch_Fakemon Date: Mon, 29 Dec 2025 20:27:50 -0600 Subject: [PATCH 3/6] =?UTF-8?q?did=20the=20regex=20block=20name=20change?= =?UTF-8?q?=20thing=F0=9F=91=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/text.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/text.js b/extensions/text.js index 210bbad90e..51cfc37836 100644 --- a/extensions/text.js +++ b/extensions/text.js @@ -314,7 +314,7 @@ opcode: "matchRegexJSON", blockType: Scratch.BlockType.REPORTER, text: Scratch.translate({ - default: "[STRING] matched by regex /[REGEX]/[FLAGS] as array", + default: "matches of [STRING] using regex /[REGEX]/[FLAGS] as array", description: "/[REGEX]/ is supposed to match the syntax that some actual programming languages used for regular expressions.", }), From 8eb6ebd19821a7526c88a3ccec6f1a0663b615f5 Mon Sep 17 00:00:00 2001 From: "DangoCat[bot]" Date: Tue, 30 Dec 2025 02:29:33 +0000 Subject: [PATCH 4/6] [Automated] Format code --- extensions/text.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extensions/text.js b/extensions/text.js index 51cfc37836..01790d9a36 100644 --- a/extensions/text.js +++ b/extensions/text.js @@ -314,7 +314,8 @@ opcode: "matchRegexJSON", blockType: Scratch.BlockType.REPORTER, text: Scratch.translate({ - default: "matches of [STRING] using regex /[REGEX]/[FLAGS] as array", + default: + "matches of [STRING] using regex /[REGEX]/[FLAGS] as array", description: "/[REGEX]/ is supposed to match the syntax that some actual programming languages used for regular expressions.", }), From 5008cc7e1978dc72882feccfcd77a2dbfaf29f7c Mon Sep 17 00:00:00 2001 From: Scratch_Fakemon Date: Tue, 30 Dec 2025 08:39:51 -0600 Subject: [PATCH 5/6] Remove splitJSON (JSON extension already has it) --- extensions/text.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/extensions/text.js b/extensions/text.js index 01790d9a36..735c736df9 100644 --- a/extensions/text.js +++ b/extensions/text.js @@ -139,21 +139,6 @@ }, }, }, - { - opcode: "splitJSON", - blockType: Scratch.BlockType.REPORTER, - text: Scratch.translate("[STRING] split by [SPLIT] as array"), - arguments: { - STRING: { - type: Scratch.ArgumentType.STRING, - defaultValue: "apple", - }, - SPLIT: { - type: Scratch.ArgumentType.STRING, - defaultValue: "p", - }, - }, - }, { opcode: "count", blockType: Scratch.BlockType.REPORTER, From c3719038bc02dbfd4221cad9b81581f9f15c9a59 Mon Sep 17 00:00:00 2001 From: SharkPool <139097378+SharkPool-SP@users.noreply.github.com> Date: Tue, 30 Dec 2025 11:42:30 -0800 Subject: [PATCH 6/6] unused function --- extensions/text.js | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/extensions/text.js b/extensions/text.js index 735c736df9..f701a0a2c8 100644 --- a/extensions/text.js +++ b/extensions/text.js @@ -571,29 +571,6 @@ } return splitCache.arr[item - 1] || ""; } - splitJSON(args, util) { - // split but it returns an array - const string = Scratch.Cast.toString(args.STRING); - const split = Scratch.Cast.toString(args.SPLIT); - - // Cache the last split - if ( - !( - splitCache && - splitCache.string === string && - splitCache.split === split - ) - ) { - const regex = this._caseInsensitiveRegex(split); - - splitCache = { - string, - split, - arr: string.split(regex), - }; - } - return JSON.stringify(splitCache.arr) || "[]"; - } count(args, util) { // Fill cache