2016-08-03 67 views
0

String.raw似乎是一个正确的方式来编写嵌入式命令状如何打印“ 1”使用ES6 String.raw

const command = String.raw`sed -n 's/${hash} \(.*\)/\1/p' 

但是,这是行不通的,因为\1不能在字符串写入.RAW,问题如下所示:

我可以看到
console.log(String.raw`\1`) 
=>SyntaxError: Octal literals are not allowed in template strings. 

console.log(String.raw`\\1`) 
\\1 
+1

有这将允许这个建议:https://github.com/tc39/proposal-template-literal-revision – Bakkot

回答

1

只有这样做,就

let x = '\\1' 
const command = String.raw`sed -n 's/${hash} \(.*\)/${x}/p'` 

@HBP一lmost在意见中提出的正确的判罚

const command = String.raw`sed -n 's/${hash} \(.*\)/${'\\1'}/p'` 

看起来是理想的

+0

我想想这也是唯一的解决方法,谢谢 – mko

+0

看来,你可以用''\ 1''替换'x',不需要新变量 – HBP

+0

你是什么意思@HBP? '$ { '\ 1'}'?不太......但是,'$ {'\\ 1'}'工作 –