30秒学会 JavaScript 片段 · 2023年11月17日

30秒学会 JavaScript 片段 – Prevent a string from being escaped in JavaScript

By default, when JavaScript sees an escape character (\), it will escape the character after it. However, there are cases where you might not want this behavior (e.g. when you want to store a Windows path as a string). For these cases, you can use a template literal and the String.raw() tag function:

代码实现

const path = `C:\web\index.html`; // 'C:web.html'

const unescapedPath = String.raw`C:\web\index.html`; // 'C:\web\index.html'

翻译自:https://www.30secondsofcode.org/js/s/prevent-string-being-escaped