Created
December 3, 2024 15:52
-
-
Save tluyben/fc959d7ae821c8864cf13d5af319c81e to your computer and use it in GitHub Desktop.
regex in oK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function rx(x, y, env) { | |
function isNumber(str) { | |
if (typeof str !== "string") return false; // Must be a string | |
if (str.trim() === "") return false; // Empty string isn't a number | |
return !isNaN(str) && isFinite(Number(str)); | |
} | |
x = l(x) | |
.v.map((t) => String.fromCharCode(t.v)) | |
.join(""); | |
y = l(y) | |
.v.map((t) => String.fromCharCode(t.v)) | |
.join(""); | |
const rex = new RegExp(x, "g"); | |
const matches = []; | |
for (const match of y.matchAll(rex)) { | |
matches.push( | |
k(3, [ | |
k(0, match.index), | |
k( | |
3, | |
match[0].split("").map((m) => k(1, m.charCodeAt(0))) | |
), | |
k( | |
3, | |
match.slice(1).map((m) => | |
isNumber(m) | |
? k(0, parseFloat(m)) | |
: k( | |
3, | |
m.split("").map((m1) => k(1, m1.charCodeAt(0))) | |
) | |
) | |
), | |
]) | |
); | |
} | |
return k(3, matches); | |
} | |
verbs["rx"] = [null, null, null, null, null, rx, null, null]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment