Forum Moderators: open

Message Too Old, No Replies

parenthetical backref $0.02

trouble accessing backref

         

macromike

8:08 pm on Feb 2, 2004 (gmt 0)

10+ Year Member



I am building an actionscript code highlighter using javascript, but I can't figure out how to access a back reference. Basically what I'm doing here is searching for string literals "", and wrapping them with html color fomatting. But my backref comes through as not defined. Any ideas? regexp N00b if you will :-D

function highlightSyntax() {
codeStr = "getFunction(\"abc\", \"efg\", \"hijk\")";
codeStr = codeStr.replace(/("[^"]*")/g, "<font color=\"#0000FF\">"+$1+"</font>");
return(codeStr);
}

DrDoc

8:13 pm on Feb 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World!

Can you give an example of a sentence and which words you are trying to target?

macromike

8:18 pm on Feb 2, 2004 (gmt 0)

10+ Year Member



sure. Well the words will vary dramatically, and there is no set standard on how the code will be coming in. Essentially this will be a panel that is opened inside of the flash IDE.

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

macromike

8:19 pm on Feb 2, 2004 (gmt 0)

10+ Year Member



The only string here that would be converted would be the "onEvent" in the last line, so that was probably a bad example, but you get the point :-)

DrDoc

8:20 pm on Feb 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aah! How about this:

function highlightSyntax() {
codeStr = "getFunction(\"abc\", \"efg\", \"hijk\")";
codeStr = codeStr.replace(/(\"[^\"]*\")/g, "<font color=\"#0000FF\">$1</font>");
return(codeStr);
}

macromike

8:31 pm on Feb 2, 2004 (gmt 0)

10+ Year Member



that seems to work when I alert it, which I assume means the javascript is doing it's part correctly. What was I doing wrong?

macromike

9:14 pm on Feb 2, 2004 (gmt 0)

10+ Year Member



How would I build a regExp to find all string that aren't inside of single line or multiline comments?

/*"not this string"*/ "but this one!" :-)

macromike

9:25 pm on Feb 2, 2004 (gmt 0)

10+ Year Member



\ [ ^ /\* ] *( " [ ^ " ] * " )[ ^ \*/ ]/g

is that right?

DrDoc

9:27 pm on Feb 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You know what... I was just working on that, but I can only get it to exclude one string:

/* "these words" will not be in blue */
but "these words" will

/* "these" "words" should not be "blue", but they are */
"these" "ones" are "blue too"

DrDoc

11:41 pm on Feb 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, if you ditch the font tags and use styled spans it gets a whole lot easier:


<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
function colorcode() {
srcText = document.getElementById('code').innerHTML;
re = /<br>/igm;
srcText = srcText.replace(re,"\n");
//re = / /gm;
//srcText = srcText.replace(re,"&nbsp;");

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>