简体   繁体   English

未捕获的SyntaxError:意外的令牌;

[英]Uncaught SyntaxError : Unexpected token ;

I am getting Uncaught SyntaxError: Unexpected token ; 我得到Uncaught SyntaxError: Unexpected token ; at THE LINE NUMBER 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 /</ . 在撰写本文时,SO的突出显示了原文的问题:正则表达式似乎是/</

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. 由于Javascript使用正斜杠来分隔正则表达式,因此必须使用反斜杠转义其中的任何正斜杠。


IMO, using forward slashes for regexes was the most unfortunate syntax decision of JavaScript: 使用正斜率的正则数据库的IMO是JavaScript最不幸的语法决定:

  1. Parsing JavaScript is difficult because of / starting multiline comments, single line comments, division, and regexes. 由于/启动多行注释,单行注释,除法和正则表达式,解析JavaScript很困难。 (Sublime, my editor choice, gets it wrong. Dreamweaver gets it wrong.) (Sublime,我的编辑选择,错了.Dreamweaver错了。)

  2. It makes regexes for URIs/URLs particularly ugly. 它使URI / URL的正则表达式特别难看。

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 你需要在Javascript中转义/使用\\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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