有人知道這個JavaScript錯誤來自哪裏嗎?SyntaxError:預期表達式,得到'。'
SyntaxError: expected expression, got '.'
我使用正則表達式中包含el.href.match(/video\/(.*)@/)[1];
傳遞給像一個createTextNode,或的textContent的innerHTML函數的字符串斜槓(逃脫)時出現此錯誤。
這個正則表達式不作爲文本存儲時工作。 沒有斜槓文本工程的正則表達式,你可以看到我的代碼示例:
HTML:
<a style="display: none; width: 840px; height: 472px;" href="http://videos.francetv.fr/video/[email protected]" id="catchup" class="video"></a>
的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);
工作和失敗的測試可以在這裏找到:
你可以測試它住在GitHub上的頁面(的jsfiddle並沒有我的情況下工作):
https://baptx.github.io/get_m3u8_debug/get_m3u8_debug.htm
你應該直接通過了相關代碼的問題(如果你刪除回購)和正確的鏈接應指向一個特定的版本,而不是到主分支 - [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) – Aprillion
@Aprillion謝謝,我想過在StackOverflow上添加代碼,但我希望人們也能看到所有的工作案例。通常我不會刪除回購,但我有想法備份Wayback Machine上的鏈接https://web.archive.org/web/20161022102615/https://baptx.github.io/get_m3u8_debug/get_m3u8_debug.htm 如果我想修正錯誤,請在特定版本的好處。 – baptx