Uncaught SyntaxError : Unexpected token ;

前端 未结 2 481
隐瞒了意图╮
隐瞒了意图╮ 2021-01-24 17:09

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

    // HTML Helper
    var documentHtml = function(html){
            


        
相关标签:
2条回答
  • 2021-01-24 17:46

    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

    0 讨论(0)
  • 2021-01-24 17:49

    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.

    0 讨论(0)
提交回复
热议问题