1

I am getting Uncaught SyntaxError: Unexpected token ; at THE LINE NUMBER

 // HTML Helper var documentHtml = function(html){ // Prepare var result = String(html) .replace(/<!DOCTYPE[^>]*>/i, '') .replace(/<(html|head|body|title|meta|script)([s>])/gi,'<div class="document-$1"$2') .replace(/</(html|head|body|title|meta|script)>/gi,'</div>') ; // << THE LINE NUMBER // Return return $.trim(result); }; 

Not sure what is wrong.

2 Answers 2

2

The problem is:

/</(html|head|body|title|meta|script)>/gi 

At the time of writing, SO's highlighting shows the problem with the original: the regex seems to be /</.

It should be:

/<\/(html|head|body|title|meta|script)>/gi 

Since Javascript uses forward slashes to delimit regexes, you have to escape any forward slash inside it with a backslash.


IMO, using forward slashes for regexes was the most unfortunate syntax decision of JavaScript:

  1. Parsing JavaScript is difficult because of / starting multiline comments, single line comments, division, and regexes. (Sublime, my editor choice, gets it wrong. Dreamweaver gets it wrong.)

  2. It makes regexes for URIs/URLs particularly ugly.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, I think it was a really great syntax decision. =) It’s inconvenient for parsing URLs and HTML – two things that should never really be parsed with regular expressions.
@minitech, what about matching URL paths, e.g. /^\/some\/path\/prefix\/.*/.test(path) (which I believe is what the OP is doing). What's wrong with that?
That looks like URL paths to you? /<(html|head|body|title|meta|script)>/?
1

Try to change:

.replace(/</(html|head|body|title|meta|script)>/gi,'</div>') 

to:

.replace(/<\/(html|head|body|title|meta|script)>/gi,'<\/div>') 

You need to escape / with \ in Javascript

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.