HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Prevent a string from being escaped in JavaScript

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptfromescapedbeingpreventstring

Problem

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:

Solution

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

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

Code Snippets

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

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

Context

From 30-seconds-of-code: prevent-string-being-escaped

Revisions (0)

No revisions yet.