问题描述:

What the code does is to take a string and replace the content inside using a regex and a replacement:

#!/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()

I'm getting the following error, though:

/home/alex/node/es6/index.js:7

for (let [regex, replacement] of replacementArray) {

^

SyntaxError: Unexpected token [

I'm not sure why this is happening since replacementArray is effectively an array inside the transformHeadings function.

Note: I'm using io.js.

网友答案:

Personally, I have not been successful in using a let inside of a for with array comprehension. Consider the following example:

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

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

I have had great success once the let is removed. This produces the expected output:

3
7
11
相关阅读:
Top