2015-03-31 69 views
3

什麼代碼確實是採取了串並替換裏面的內容使用regexreplacement爲什麼我得到意想不到的標記[在下面爲...循環?

#!/usr/bin/env node 

'use strict' 

function massReplace(text, replacementArray) { 
    let results = text 
    for (let [regex, replacement] of replacementArray) { 
    results = results.replace(regex, replacement) 
    } 
    return results 
} 

function transformHeadings() { 
    let text = `# Title 

## Title` 

    massReplace(text, 
    [ [ /\*\*([^\*|\s]+)\*\*/g, '<strong>$1</strong>'], 
    [ /\*([^\*|\s]+)\*/g,  '<em>$1</em>' ] ] 
) 
} 

transformHeadings() 

我收到以下錯誤,但:

/home/alex/node/es6/index.js:7 
    for (let [regex, replacement] of replacementArray) { 
     ^
SyntaxError: Unexpected token [ 

我不知道爲什麼會發生這種情況,因爲replacementArray實際上是transformHeadings函數中的一個數組。

注意:我正在使用io.js

+1

您是否能夠在其他代碼中使用Node中的解構賦值? *編輯* - V8的bug:https://code.google.com/p/v8/issues/detail?id=811 – Pointy 2015-03-31 13:20:06

+2

根據https://kangax.github.io/compat-table/es6/,解構是尚未在Node中支持。 – 2015-03-31 13:24:04

+0

分號似乎有點亮...... – 2015-03-31 13:35:36

回答

1

就個人而言,我沒有成功地使用for內的let排序理解。考慮下面的例子:

var arr=[[1,2],[3,4],[5,6]]; 

for([a,b] of arr) { 
    console.log(a+b); 
} 

一旦let被刪除,我獲得了巨大的成功。這會產生預期的輸出:

3 
7 
11 
+1

它沒有工作。它表示''是'意外的標識符'。 – alexchenco 2015-03-31 13:29:27

+0

好的。讓我粘貼更多示例代碼。不掛斷 – 2015-03-31 13:31:14

相關問題