0

I was using the following code to detect all the javadoc comments in the pre elements to replace them with a color of my choice.

<script type="text/javascript">
    var arr = document.getElementsByTagName("pre");
    for (var i=0; i<arr.length; i++){
        arr[i].innerHTML = arr[i].innerHTML.replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;');
        arr[i].innerHTML = arr[i].innerHTML.replace(/</g, '&lt;');
        arr[i].innerHTML = arr[i].innerHTML.replace(/>/g, '&gt;');
        arr[i].innerHTML = arr[i].innerHTML.replace(/\\/*\*\*([^\*]|\*(?!/))*\\*/g, '<font color="#3F5FBF">' + $& + '</font>');
    }
</script>

but it gives me the following error.

Uncaught SyntaxError: Unexpected token ILLEGAL 

EDIT: changed the code to

<script type="text/javascript">
var arr = document.getElementsByTagName("pre");
for (var i=0; i<arr.length; i++){
    arr[i].innerHTML = arr[i].innerHTML.replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;');
    arr[i].innerHTML = arr[i].innerHTML.replace(/</g, '&lt;');
    arr[i].innerHTML = arr[i].innerHTML.replace(/>/g, '&gt;');
    var reg = /\\\/*\*\*([^\*]|\*(?!\/))*\\*/;
    var cds = arr[i].innerHTML.match(reg);
    for (var j=0; j<cds.length; j++){
        arr[i].innerHTML = arr[i].innerHTML.replace(cds[j], '<font color="#3F5FBF">' + cds[j] + '</font>'); 
    }
}
prettyPrint();
</script>

But the error changes to

Uncaught TypeError: Cannot read property 'length' of null start-ahead.html:234

(anonymous function)

5
  • 3
    The unexpected token is probably \ after /\\/*. Look at the syntax highlighter, your escape is wrong. Commented May 30, 2012 at 1:15
  • 2
    @FelixKling is right, and there's another error in (?!/). Replace it with (?!\/) Commented May 30, 2012 at 1:19
  • @FelixKling is right, OP is escaping the escape character and not escaping the scope character. Just as Mark explained in his answer. +1 for Mark. Commented May 30, 2012 at 1:19
  • Syntax errors are something you should be able to fix by yourself. SO is not your personal debugger. Your expression is still incorrect. Commented May 30, 2012 at 1:24
  • Why don't you take a look at the 234 line of your code start-ahead.html:234 Commented May 30, 2012 at 1:27

2 Answers 2

4

The problem is here /\\/*\*\*([^\*]|\*(?!/))*\\*/g, when you used the \\ and then used /. The program understands that your regex is like this /\\/ and what comes after results in an error. Try this regex:

/\/*\*\*([^\*]|\*(?!\/))*\\*/g

Or, depending on what you want:

/\\\/*\*\*([^\*]|\*(?!\/))*\\*/g
Sign up to request clarification or add additional context in comments.

Comments

1

The replace() method needs a string as a second parameter. The patterns in it to be replaced with captures need to be in that string.

You try to build that string out of two string literals and a variable named "$&". Yet, that is an illegal name - the "&" is a bitwise AND operator and lacks a second operand, because it is followed by "+" - SyntaxError. will produce an horrible runtime error.

Also, as @FelixKling mentioned, your regexp is badly escaped. It might should be something like new Regexp("\\/*\*\*([^\*]|\*(?!/))*\\*", "g"), yet then there were some useless backslashes. What do you want to approach? Is it /\*{3}([^*]|\*(?!\/))\**/g?

2 Comments

@Mark: It won't produce this error, but it will nevertheless be a problem once the expression is fixed.
This operation does not create an error though. Assuming $ is defined, 'a' + $& + 'b' just results in 0. I assume the whole expression is evaluated as 'a' + $ & (+'b') (the second + being an unary plus).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.