简体   繁体   中英

Uncaught SyntaxError : Unexpected token ;

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM