简体   繁体   English

SyntaxError:预期的表达式,得到了'。

[英]SyntaxError: expected expression, got '.'

Does someone know where this JavaScript error comes from? 有人知道这个JavaScript错误的来源吗?

SyntaxError: expected expression, got '.'

I get this error when using a regular expression with a slash (escaped) like el.href.match(/video\\/(.*)@/)[1]; 使用带有正斜杠(转义)的正则表达式时会出现此错误,例如el.href.match(/video\\/(.*)@/)[1]; as a string passed to a function like createTextNode, textContent or innerHTML. 作为传递给诸如createTextNode,textContent或innerHTML之类的函数的字符串。

This regex works when not stored as text. 当不存储为文本时,此正则表达式有效。 A regex without slash as text works, you can see my code example: 当文本有效时,不带斜杠的正则表达式,可以看到我的代码示例:

HTML: HTML:

<a style="display: none; width: 840px; height: 472px;" href="http://videos.francetv.fr/video/147338657@Info-web" id="catchup" class="video"></a>

JavaScript: JavaScript:

var el = document.getElementById("catchup");
var script = document.createElement("script");
var text = `
    (function() {
        var id = el.href.match(/video\/(.*)@/)[1];
        alert("test 4 - regex with slash as text: " + id);
    })();`; 
script.appendChild(document.createTextNode(text));      
document.getElementsByTagName("head")[0].appendChild(script);

Working and failing tests can be found here: 工作和失败的测试可以在这里找到:

https://github.com/baptx/baptx.github.io/blob/65f11b77df5a7464365374b3505921a4ef9b1272/get_m3u8_debug/get_m3u8_debug.htm https://github.com/baptx/baptx.github.io/blob/65f11b77df5a7464365374b3505921a4ef9b1272/get_m3u8_debug/get_m3u8_debug.htm

You can test it live on GitHub Pages (JSFiddle did not work in my case): 您可以在GitHub Pages上进行实时测试(在我的情况下,JSFiddle无效):

https://baptx.github.io/get_m3u8_debug/get_m3u8_debug.htm https://baptx.github.io/get_m3u8_debug/get_m3u8_debug.htm

You are escaping the forward slash instead of having a baskward slash. 您在转义正斜杠,而不是笨拙的斜杠。

`el.href.match(/video\/(.*)@/)[1]` === 'el.href.match(/video/(.*)@/)[1]'
// '\/' == '/', not '\\/'

You need to escape the backward slash as well: 您还需要转义反斜杠:

`el.href.match(/video\\/(.*)@/)[1]`

You can also take advantage of the template string with the string representation of a regex to get it's source code representation. 您还可以利用带有正则表达式字符串表示形式的模板字符串来获取其源代码表示形式。 Basically, eval(/any regex/ + '') will get the same regex. 基本上, eval(/any regex/ + '')将获得相同的regex。

var regex = /video\/(.*)@/;
var text = `el.href.match(${regex})[1]`;
// Or:
var text = `el.href.match(` + /video\/(.*)@/ + ')[1]';

/video\/(.*)@/igm + '' === '/video\\/(.*)@/gim';
new RegExp('video\\/(.*)@', 'gmi') + '' === '/video\\/(.*)@/gim';

If by "as a string" you mean "video\\/(.*)@" , then the backslash itself needs to be escaped, "\\\\" is a string literal containing one backslash: 如果用“作为字符串”表示"video\\/(.*)@" ,则需要对反斜杠本身进行转义, "\\\\"是包含一个反斜杠的字符串文字:

/video\/(.*)@/

is the same as 是相同的

new Regex("video\\/(.*)@")

You're using template literals with literal strings as regular expressions, and you have to double-escape the slashes, or any other special character, in those regular expressions. 您正在使用带有文字字符串的模板文字作为正则表达式,并且必须在这些正则表达式中两次转义斜杠或任何其他特殊字符。

Javascript interprets and removes the first escape character when parsing the string literal, and you need the escape character to make it into the script, so you'll need two of them. 解析字符串文字时,JavaScript会解释并删除第一个转义字符,并且您需要将转义字符放入脚本中,因此您将需要两个。

var text = `
        (function() {
            var id = el.href.match(/video\\/(.*)@/)[1];
            alert("test 4 - regex with slash as text: " + id);
        })();`;

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

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