Probably some easy rep points here.
I have this method:
function formatText(input) {
return input
.replaceAll(
/[#[0-9a-fA-F]{3,6}]/g,
(match) => `<span style="color:${match.slice(1, -1)}">`
)
.replaceAll(/[b]/g, '<b>')
.replaceAll(/[i]/g, '<i>')
.replaceAll(/[u]/g, '<u>')
.replaceAll(/[!b]/g, '</b>')
.replaceAll(/[!i]/g, '</i>')
.replaceAll(/[!u]/g, '</u>')
.replaceAll(/[!c]/g, '</span>');
}
It’s intended to be a simple formatting language. However, when I put any text through it (even without formatting) it doesn’t print and raises an error.
Here’s my full code:
<!doctype html>
<html lang="en">
<head>
<title>ECMAtermOS</title>
<link rel="stylesheet" href="style.css">
<script src="jquery.js" defer></script>
<script src="ecmaterm.js" defer></script>
<meta charset="utf-8">
</head>
<body>
<script defer>
addEventListener("DOMContentLoaded", _ => {
const term = new ECMAterm();
term.newTerm('body');
term.print(['[#0f0]test[!c]']);
term.print(['test 2']);
});
</script>
</body>
</html>
class ECMAterm {
constructor() {
this.terminal = /*html*/ `
<pre><code id="terminal">ECMAtermOS v1.0.0-alpha.1</code></pre>
`;
}
newTerm(containerSel) {
this.instance = $(containerSel).append(this.terminal);
this.output = $('#terminal');
}
print(args, type = 'text') {
function formatText(input) {
return input
.replaceAll(
/[#[0-9a-fA-F]{3,6}]/g,
(match) => `<span style="color:${match.slice(1, -1)}">`
)
.replaceAll(/[b]/g, '<b>')
.replaceAll(/[i]/g, '<i>')
.replaceAll(/[u]/g, '<u>')
.replaceAll(/[!b]/g, '</b>')
.replaceAll(/[!i]/g, '</i>')
.replaceAll(/[!u]/g, '</u>')
.replaceAll(/[!c]/g, '</span>');
}
for (const arg of args) {
arg = formatText(arg);
this.output.append(/*html*/ `<div class=${type}>${arg}</div>`);
}
}
}
LLMs can’t seem to fix it, which leads me to think it’s a stupid mistake I made somewhere, but I can’t find it.
Here’s how I’m using it:
addEventListener("DOMContentLoaded", _ => {
const term = new ECMAterm();
term.newTerm('body');
term.print(['[#0f0]test[!c]']); // Should return <span style="color:#0f0">test</span>
term.print(['test 2']); // Should return itself
});
Neither of the strings print.
EDIT:
Note – if I don’t run arg through formatText() it prints as normal




