未捕获的SyntaxError:意外的令牌; [英] Uncaught SyntaxError : Unexpected token ;

查看:177
本文介绍了未捕获的SyntaxError:意外的令牌;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到未捕获的SyntaxError:意外的令牌; 位于 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);
    };

不确定有什么问题。

推荐答案

问题是:

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

在撰写本文时,SO的突出显示了问题原文:正则表达式似乎是 /< /

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

它应该是:

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

由于Javascript使用正斜杠来分隔正则表达式,因此必须使用反斜杠转义其中的任何正斜杠。

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

IMO,使用正斜面的正斜杠是JavaScript最不幸的语法决定:

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


  1. 解析JavaScript很困难,因为 / 启动多行注释,单行注释,除法和正则表达式。 (Sublime,我的编辑选择,错了。 Dreamweaver 弄错了。 )

  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.)

它使URI / URL的正则表达式特别难看。

It makes regexes for URIs/URLs particularly ugly.

这篇关于未捕获的SyntaxError:意外的令牌;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆