Forum Moderators: open
function highlightSyntax() {
codeStr = "getFunction(\"abc\", \"efg\", \"hijk\")";
codeStr = codeStr.replace(/("[^"]*")/g, "<font color=\"#0000FF\">"+$1+"</font>");
return(codeStr);
}
Basically, I need to format all strings blue, reserved words green and comments to magenta.
Here's something that might be entered into the codebank that would need to return with formatting. (I am working on strings now bc I figured that would be easiest):
////////////////////////////////////////////////////////////////////////
// Changes ASBroadcaster.initialize method so that the intialized object can add/remove several listeners with one function call.
//Covenent / proto.layer51.com
//7.05.03
////////////////////////////////////////////////////////////////////////
ASBroadcaster.old_initialize = ASBroadcaster.initialize;
ASBroadcaster.initialize = function (obj) {
ASBroadcaster.old_initialize(obj);
obj.addListeners = function () {
for (var a=0; a<arguments.length; a++) {
this.addListener(arguments[a]);
}
}
obj.removeListeners = function () {
for (var a=0; a<arguments.length; a++) {
this.removeListener(arguments[a]);
}
}
}
////////////////////////////////////////////////////////////////////////
// Usage
////////////////////////////////////////////////////////////////////////
ASBroadcaster.initialize(myClip);
myClip.addListeners(clipA, clipB, clipC, clipD);
myClip.removeListeners(clipB, clipD);
myClip.broadcastMessage("onEvent", this); // broadcasts message to clipA and clipC
re = /(\/\/[^\n]*)/gm
srcText = srcText.replace(re,"<span class='comment'>$1</span>");
re = /(\/\*[^(\*\/)]*\*\/)/gm
srcText = srcText.replace(re,"<span class='comment'>$1</span>");
re = /(\"[^\"]*\")/gm;
srcText = srcText.replace(re,"<span class='string'>$1</span>");
re = /([\W])([0-9\.]+)([\W])/gm;
srcText = srcText.replace(re,"$1<span class='num'>$2</span>$3");
//alert(srcText);
re = /\n/gm;
srcText = srcText.replace(re,"<br>");
document.getElementById('code').innerHTML = srcText;
}
</script>
<style type="text/css">
#code {
font: 12px monospace;
}
.string{ color: #00c; }
.comment{ color: #090; }
.num{ color: #c0c; }
.comment .string{ color: #090; }
.comment .num{ color: #090; }
.string .num{ color: #00c; }
</style>
</head>
<body onload="colorcode()">
<div id="code">
//this is a comment<br>
function whatever() {<br>
test = "foo1234bar";<br>
alert(test);<br>
/*<br>
multiline 1234 comment<br>
*/<br>
}<br>
/* more "comment" stuff<br>
*/<br>
"test"<br>
// more "and" more "comments"<br>
myint = 1234;<br>
/*<br>
multi<br>
"line" comments<br>
with "some" strings<br>
*/<br>
*/<br>
</div>
</body>
</html>