-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathescape-sequences.html
More file actions
82 lines (69 loc) · 30.4 KB
/
escape-sequences.html
File metadata and controls
82 lines (69 loc) · 30.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>JavsScript - Escape Sequences</title>
<meta name="generator" content="VuePress 1.8.2">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="description" content="Entering special characters in strings and regular expressions, Escape sequence types">
<meta property="og:site_name" content="DevTut">
<meta property="og:title" content="JavsScript - Escape Sequences">
<meta property="og:description" content="Entering special characters in strings and regular expressions, Escape sequence types">
<meta property="og:type" content="article">
<meta property="og:url" content="/javascript/escape-sequences.html">
<meta property="og:image" content="/logo.png">
<meta name="twitter:title" content="JavsScript - Escape Sequences">
<meta name="twitter:description" content="Entering special characters in strings and regular expressions, Escape sequence types">
<meta name="twitter:url" content="/javascript/escape-sequences.html">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="/logo.png">
<meta name="theme-color" content="#ffffff">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="msapplication-TileImage" content="/mstile-150x150.png">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="google-site-verification" content="76_rKXgwMVIjd-axJC_1zPV9OS4mEjvtgjYOWVkAdnQ">
<link rel="preload" href="/assets/css/0.styles.60619e34.css" as="style"><link rel="preload" href="/assets/js/app.1779e102.js" as="script"><link rel="preload" href="/assets/js/3.2cfa8016.js" as="script"><link rel="preload" href="/assets/js/1737.3e143ee1.js" as="script">
<link rel="stylesheet" href="/assets/css/0.styles.60619e34.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">DevTut</span></a> <div class="links"><form id="search-form" role="search" class="algolia-search-wrapper search-box"><input id="algolia-search-input" class="search-query"></form> <nav class="nav-links can-hide"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>JavaScript</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/javascript/" aria-current="page" class="sidebar-link">Disclaimer</a></li><li><a href="/javascript/getting-started-with-javascript.html" class="sidebar-link">Getting started with JavaScript</a></li><li><a href="/javascript/javascript-variables.html" class="sidebar-link">JavaScript Variables</a></li><li><a href="/javascript/built-in-constants.html" class="sidebar-link">Built-in Constants</a></li><li><a href="/javascript/comments.html" class="sidebar-link">Comments</a></li><li><a href="/javascript/console.html" class="sidebar-link">Console</a></li><li><a href="/javascript/datatypes-in-javascript.html" class="sidebar-link">Datatypes in Javascript</a></li><li><a href="/javascript/strings.html" class="sidebar-link">Strings</a></li><li><a href="/javascript/date.html" class="sidebar-link">Date</a></li><li><a href="/javascript/date-comparison.html" class="sidebar-link">Date Comparison</a></li><li><a href="/javascript/comparison-operations.html" class="sidebar-link">Comparison Operations</a></li><li><a href="/javascript/conditions.html" class="sidebar-link">Conditions</a></li><li><a href="/javascript/arrays.html" class="sidebar-link">Arrays</a></li><li><a href="/javascript/objects.html" class="sidebar-link">Objects</a></li><li><a href="/javascript/arithmetic-math.html" class="sidebar-link">Arithmetic (Math)</a></li><li><a href="/javascript/bitwise-operators.html" class="sidebar-link">Bitwise operators</a></li><li><a href="/javascript/constructor-functions.html" class="sidebar-link">Constructor functions</a></li><li><a href="/javascript/declarations-and-assignments.html" class="sidebar-link">Declarations and Assignments</a></li><li><a href="/javascript/loops.html" class="sidebar-link">Loops</a></li><li><a href="/javascript/functions.html" class="sidebar-link">Functions</a></li><li><a href="/javascript/functional-javascript.html" class="sidebar-link">Functional JavaScript</a></li><li><a href="/javascript/prototypes-objects.html" class="sidebar-link">Prototypes, objects</a></li><li><a href="/javascript/classes.html" class="sidebar-link">Classes</a></li><li><a href="/javascript/namespacing.html" class="sidebar-link">Namespacing</a></li><li><a href="/javascript/context-this.html" class="sidebar-link">Context (this)</a></li><li><a href="/javascript/setters-and-getters.html" class="sidebar-link">Setters and Getters</a></li><li><a href="/javascript/events.html" class="sidebar-link">Events</a></li><li><a href="/javascript/inheritance.html" class="sidebar-link">Inheritance</a></li><li><a href="/javascript/method-chaining.html" class="sidebar-link">Method Chaining</a></li><li><a href="/javascript/callbacks.html" class="sidebar-link">Callbacks</a></li><li><a href="/javascript/intervals-and-timeouts.html" class="sidebar-link">Intervals and Timeouts</a></li><li><a href="/javascript/regular-expressions.html" class="sidebar-link">Regular expressions</a></li><li><a href="/javascript/cookies.html" class="sidebar-link">Cookies</a></li><li><a href="/javascript/web-storage.html" class="sidebar-link">Web Storage</a></li><li><a href="/javascript/data-attributes.html" class="sidebar-link">Data attributes</a></li><li><a href="/javascript/json.html" class="sidebar-link">JSON</a></li><li><a href="/javascript/ajax.html" class="sidebar-link">AJAX</a></li><li><a href="/javascript/enumerations.html" class="sidebar-link">Enumerations</a></li><li><a href="/javascript/map.html" class="sidebar-link">Map</a></li><li><a href="/javascript/timestamps.html" class="sidebar-link">Timestamps</a></li><li><a href="/javascript/unary-operators.html" class="sidebar-link">Unary Operators</a></li><li><a href="/javascript/generators.html" class="sidebar-link">Generators</a></li><li><a href="/javascript/promises.html" class="sidebar-link">Promises</a></li><li><a href="/javascript/set.html" class="sidebar-link">Set</a></li><li><a href="/javascript/modals-prompts.html" class="sidebar-link">Modals - Prompts</a></li><li><a href="/javascript/execcommand-and-contenteditable.html" class="sidebar-link">execCommand and contenteditable</a></li><li><a href="/javascript/history.html" class="sidebar-link">History</a></li><li><a href="/javascript/navigator-object.html" class="sidebar-link">Navigator Object</a></li><li><a href="/javascript/bom-browser-object-model.html" class="sidebar-link">BOM (Browser Object Model)</a></li><li><a href="/javascript/the-event-loop.html" class="sidebar-link">The Event Loop</a></li><li><a href="/javascript/strict-mode.html" class="sidebar-link">Strict mode</a></li><li><a href="/javascript/custom-elements.html" class="sidebar-link">Custom Elements</a></li><li><a href="/javascript/data-manipulation.html" class="sidebar-link">Data Manipulation</a></li><li><a href="/javascript/binary-data.html" class="sidebar-link">Binary Data</a></li><li><a href="/javascript/template-literals.html" class="sidebar-link">Template Literals</a></li><li><a href="/javascript/fetch.html" class="sidebar-link">Fetch</a></li><li><a href="/javascript/scope.html" class="sidebar-link">Scope</a></li><li><a href="/javascript/modules.html" class="sidebar-link">Modules</a></li><li><a href="/javascript/screen.html" class="sidebar-link">Screen</a></li><li><a href="/javascript/variable-coercion-conversion.html" class="sidebar-link">Variable coercion/conversion</a></li><li><a href="/javascript/destructuring-assignment.html" class="sidebar-link">Destructuring assignment</a></li><li><a href="/javascript/websockets.html" class="sidebar-link">WebSockets</a></li><li><a href="/javascript/arrow-functions.html" class="sidebar-link">Arrow Functions</a></li><li><a href="/javascript/workers.html" class="sidebar-link">Workers</a></li><li><a href="/javascript/requestanimationframe.html" class="sidebar-link">requestAnimationFrame</a></li><li><a href="/javascript/creational-design-patterns.html" class="sidebar-link">Creational Design Patterns</a></li><li><a href="/javascript/detecting-browser.html" class="sidebar-link">Detecting browser</a></li><li><a href="/javascript/symbols.html" class="sidebar-link">Symbols</a></li><li><a href="/javascript/transpiling.html" class="sidebar-link">Transpiling</a></li><li><a href="/javascript/automatic-semicolon-insertion-asi.html" class="sidebar-link">Automatic Semicolon Insertion - ASI</a></li><li><a href="/javascript/localization.html" class="sidebar-link">Localization</a></li><li><a href="/javascript/geolocation.html" class="sidebar-link">Geolocation</a></li><li><a href="/javascript/indexeddb.html" class="sidebar-link">IndexedDB</a></li><li><a href="/javascript/modularization-techniques.html" class="sidebar-link">Modularization Techniques</a></li><li><a href="/javascript/proxy.html" class="sidebar-link">Proxy</a></li><li><a href="/javascript/postmessage-and-messageevent.html" class="sidebar-link">.postMessage() and MessageEvent</a></li><li><a href="/javascript/weakmap.html" class="sidebar-link">WeakMap</a></li><li><a href="/javascript/weakset.html" class="sidebar-link">WeakSet</a></li><li><a href="/javascript/escape-sequences.html" aria-current="page" class="active sidebar-link">Escape Sequences</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/javascript/escape-sequences.html#entering-special-characters-in-strings-and-regular-expressions" class="sidebar-link">Entering special characters in strings and regular expressions</a></li><li class="sidebar-sub-header"><a href="/javascript/escape-sequences.html#escape-sequence-types" class="sidebar-link">Escape sequence types</a></li></ul></li><li><a href="/javascript/behavioral-design-patterns.html" class="sidebar-link">Behavioral Design Patterns</a></li><li><a href="/javascript/server-sent-events.html" class="sidebar-link">Server-sent events</a></li><li><a href="/javascript/async-functions-async-await.html" class="sidebar-link">Async functions (async/await)</a></li><li><a href="/javascript/async-iterators.html" class="sidebar-link">Async Iterators</a></li><li><a href="/javascript/how-to-make-iterator-usable-inside-async-callback-function.html" class="sidebar-link">How to make iterator usable inside async callback function</a></li><li><a href="/javascript/tail-call-optimization.html" class="sidebar-link">Tail Call Optimization</a></li><li><a href="/javascript/bitwise-operators-real-world-examples-snippets.html" class="sidebar-link">Bitwise Operators - Real World Examples (snippets)</a></li><li><a href="/javascript/tilde.html" class="sidebar-link">Tilde ~</a></li><li><a href="/javascript/using-javascript-to-get-set-css-custom-variables.html" class="sidebar-link">Using javascript to get/set CSS custom variables</a></li><li><a href="/javascript/selection-api.html" class="sidebar-link">Selection API</a></li><li><a href="/javascript/file-api-blobs-and-filereaders.html" class="sidebar-link">File API, Blobs and FileReaders</a></li><li><a href="/javascript/notifications-api.html" class="sidebar-link">Notifications API</a></li><li><a href="/javascript/vibration-api.html" class="sidebar-link">Vibration API</a></li><li><a href="/javascript/battery-status-api.html" class="sidebar-link">Battery Status API</a></li><li><a href="/javascript/fluent-api.html" class="sidebar-link">Fluent API</a></li><li><a href="/javascript/web-cryptography-api.html" class="sidebar-link">Web Cryptography API</a></li><li><a href="/javascript/security-issues.html" class="sidebar-link">Security issues</a></li><li><a href="/javascript/same-origin-policy-cross-origin-communication.html" class="sidebar-link">Same Origin Policy & Cross-Origin Communication</a></li><li><a href="/javascript/error-handling.html" class="sidebar-link">Error Handling</a></li><li><a href="/javascript/global-error-handling-in-browsers.html" class="sidebar-link">Global error handling in browsers</a></li><li><a href="/javascript/debugging.html" class="sidebar-link">Debugging</a></li><li><a href="/javascript/unit-testing-javascript.html" class="sidebar-link">Unit Testing Javascript</a></li><li><a href="/javascript/evaluating-javascript.html" class="sidebar-link">Evaluating JavaScript</a></li><li><a href="/javascript/linters-ensuring-code-quality.html" class="sidebar-link">Linters - Ensuring code quality</a></li><li><a href="/javascript/anti-patterns.html" class="sidebar-link">Anti-patterns</a></li><li><a href="/javascript/performance-tips.html" class="sidebar-link">Performance Tips</a></li><li><a href="/javascript/memory-efficiency.html" class="sidebar-link">Memory efficiency</a></li><li><a href="/javascript/reserved-keywords.html" class="sidebar-link">Reserved Keywords</a></li><li><a href="/javascript/contributors.html" class="sidebar-link">The Contributors</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="escape-sequences"><a href="#escape-sequences" class="header-anchor">#</a> Escape Sequences</h1> <h2 id="entering-special-characters-in-strings-and-regular-expressions"><a href="#entering-special-characters-in-strings-and-regular-expressions" class="header-anchor">#</a> Entering special characters in strings and regular expressions</h2> <p>Most printable characters can be included in string or regular expression literals just as they are, e.g.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">"ポケモン"</span><span class="token punctuation">;</span> <span class="token comment">// a valid string</span>
<span class="token keyword">var</span> regExp <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[Α-Ωα-ω]</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span> <span class="token comment">// matches any Greek letter without diacritics</span>
</code></pre></div><p>In order to add arbitrary characters to a string or regular expression, including non-printable ones, one has to use <strong>escape sequences</strong>. Escape sequences consist of a backslash ("") followed by one or more other characters. To write an escape sequence for a particular character, one typically (but not always) needs to know its hexadecimal <a href="http://stackoverflow.com/documentation/javascript/1041/strings/19135/character-code#t=201608112028595843342" target="_blank" rel="noopener noreferrer">character code<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>.</p> <p>JavaScript provides a number of different ways to specify escape sequences, as documented in the examples in this topic. For instance, the following escape sequences all denote the same character: the <strong>line feed</strong> (Unix newline character), with character code U+000A.</p> <ul><li><code>\n</code></li> <li><code>\x0a</code></li> <li><code>\u000a</code></li> <li><code>\u{a}</code> new in ES6, only in strings</li> <li><code>\012</code> forbidden in string literals in strict mode and in template strings</li> <li><code>\cj</code> only in regular expressions</li></ul> <h2 id="escape-sequence-types"><a href="#escape-sequence-types" class="header-anchor">#</a> Escape sequence types</h2> <h3 id="single-character-escape-sequences"><a href="#single-character-escape-sequences" class="header-anchor">#</a> Single character escape sequences</h3> <p>Some escape sequences consist of a backslash followed by a single character.</p> <p>For example, in <code>alert("Hello\nWorld");</code>, the escape sequence <code>\n</code> is used to introduce a newline in the string parameter, so that the words "Hello" and "World" are displayed in consecutive lines.</p> <table><thead><tr><th>Escape sequence</th> <th>Character</th> <th>Unicode</th></tr></thead> <tbody><tr><td><code>\b</code> (only in strings, not in regular expressions)</td> <td>backspace</td> <td>U+0008</td></tr> <tr><td><code>\t</code></td> <td>horizontal tab</td> <td>U+0009</td></tr> <tr><td><code>\n</code></td> <td>line feed</td> <td>U+000A</td></tr> <tr><td><code>\v</code></td> <td>vertical tab</td> <td>U+000B</td></tr> <tr><td><code>\f</code></td> <td>form feed</td> <td>U+000C</td></tr> <tr><td><code>\r</code></td> <td>carriage return</td> <td>U+000D</td></tr></tbody></table> <p>Additionally, the sequence <code>\0</code>, when not followed by a digit between 0 and 7, can be used to escape the null character (U+0000).</p> <p>The sequences <code>\\</code>, <code>\'</code> and <code>\"</code> are used to escape the character that follows the backslash. While similar to non-escape sequences, where the leading backslash is simply ignored (i.e. <code>\?</code> for <code>?</code>), they are explicitly treated as single character escape sequences inside strings as per the specification.</p> <h3 id="hexadecimal-escape-sequences"><a href="#hexadecimal-escape-sequences" class="header-anchor">#</a> Hexadecimal escape sequences</h3> <p>Characters with codes between 0 and 255 can be represented with an escape sequence where <code>\x</code> is followed by the 2-digit hexadecimal character code. For example, the non-breaking space character has code 160 or A0 in base 16, and so it can be written as <code>\xa0</code>.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">"ONE\xa0LINE"</span><span class="token punctuation">;</span> <span class="token comment">// ONE and LINE with a non-breaking space between them</span>
</code></pre></div><p>For hex digits above 9, the letters <code>a</code> to <code>f</code> are used, in lowercase or uppercase without distinction.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> regExp1 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[\x00-xff]</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span> <span class="token comment">// matches any character between U+0000 and U+00FF</span>
<span class="token keyword">var</span> regExp2 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[\x00-xFF]</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span> <span class="token comment">// same as above</span>
</code></pre></div><h3 id="_4-digit-unicode-escape-sequences"><a href="#_4-digit-unicode-escape-sequences" class="header-anchor">#</a> 4-digit Unicode escape sequences</h3> <p>Characters with codes between 0 and 65535 (2<sup>16</sup> - 1) can be represented with an escape sequence where <code>\u</code> is followed by the 4-digit hexadecimal character code.</p> <p>For example, the Unicode standard defines the right arrow character ("→") with the number 8594, or 2192 in hexadecimal format. So an escape sequence for it would be <code>\u2192</code>.</p> <p>This produces the string "A → B":</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">"A \u2192 B"</span><span class="token punctuation">;</span>
</code></pre></div><p>For hex digits above 9, the letters <code>a</code> to <code>f</code> are used, in lowercase or uppercase without distinction.
Hexadecimal codes shorter than 4 digits must be left-padded with zeros: <code>\u007A</code> for the small letter "z".</p> <h3 id="curly-bracket-unicode-escape-sequences"><a href="#curly-bracket-unicode-escape-sequences" class="header-anchor">#</a> Curly bracket Unicode escape sequences</h3> <p>ES6 extends Unicode support to the full code range from 0 to 0x10FFFF.
In order to escape characters with code greater than 2<sup>16</sup> - 1, a new syntax for escape sequences was introduced:</p> <div class="language-js extra-class"><pre class="language-js"><code>\u<span class="token punctuation">{</span><span class="token operator">??</span><span class="token operator">?</span><span class="token punctuation">}</span>
</code></pre></div><p>Where the code in curly braces is hexadecimal representation of the code point value, e.g.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"Look! \u{1f440}"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Look! 👀</span>
</code></pre></div><p>In the example above, the code <code>1f440</code> is the hexadecimal representation of the character code of the Unicode Character <strong>Eyes</strong>.</p> <p>Note that the code in curly braces may contain any number of hex digits, as long the value does not exceed 0x10FFFF. For hex digits above 9, the letters <code>a</code> to <code>f</code> are used, in lowercase or uppercase without distinction.</p> <p>Unicode escape sequences with curly braces only work inside strings, not inside regular expressions!</p> <h3 id="octal-escape-sequences"><a href="#octal-escape-sequences" class="header-anchor">#</a> Octal escape sequences</h3> <p>Octal escape sequences are deprecated as of ES5, but they are still supported inside regular expressions and in non-strict mode also inside non-template strings. An octal escape sequence consists of one, two or three octal digits, with value between 0 and 377<sub>8</sub> = 255.</p> <p>For example, the capital letter "E" has character code 69, or 105 in base 8. So it can be represented with the escape sequence <code>\105</code>:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\105scape</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">"Fun with Escape Sequences"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
</code></pre></div><p>In strict mode, octal escape sequences are not allowed inside strings and will produce a syntax error. It is worth to note that <code>\0</code>, unlike <code>\00</code> or <code>\000</code>, is <strong>not</strong> considered an octal escape sequence, and is thus still allowed inside strings (even template strings) in strict mode.</p> <h3 id="control-escape-sequences"><a href="#control-escape-sequences" class="header-anchor">#</a> Control escape sequences</h3> <p>Some escape sequences are only recognized inside regular expression literals (not in strings). These can be used to escape characters with codes between 1 and 26 (U+0001–U+001A). They consist of a single letter A–Z (case makes no difference) preceded by <code>\c</code>. The alphabetic position of the letter after <code>\c</code> determines the character code.</p> <p>For example, in the regular expression</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">/\cG/</span><span class="token template-punctuation string">`</span></span>
</code></pre></div><p>The letter "G" (the 7th letter in the alphabet) refers to the character U+0007, and thus</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">/\cG</span><span class="token template-punctuation string">`</span></span><span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>String<span class="token punctuation">.</span><span class="token function">fromCharCode</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
</code></pre></div><h4 id="remarks"><a href="#remarks" class="header-anchor">#</a> Remarks</h4> <p>Not everything that starts with a backslash is an escape sequence.
Many characters are just not useful to escape sequences, and will simply cause a preceding backslash to be ignored.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token string">"\H\e\l\l\o"</span> <span class="token operator">===</span> <span class="token string">"Hello"</span> <span class="token comment">// true</span>
</code></pre></div><p>On the other hand, some characters like "u" and "x" will cause a syntax error when used improperly after a backslash.
The following is not a valid string literal because it contains the Unicode escape sequence prefix <code>\u</code> followed by a character that is not a valid hexadecimal digit nor a curly brace:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token string">"C:\Windows\System32\updatehandlers.dll"</span> <span class="token comment">// SyntaxError</span>
</code></pre></div><p>A backslash at the end of a line inside a string does not introduce an escape sequence, but indicates line continuation, i.e.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token string">"contin\
uation"</span> <span class="token operator">===</span> <span class="token string">"continuation"</span> <span class="token comment">// true</span>
</code></pre></div><h3 id="similarity-to-other-formats"><a href="#similarity-to-other-formats" class="header-anchor">#</a> Similarity to other formats</h3> <p>While escape sequences in JavaScript bear resemblance to other languages and formats, like C++, Java, JSON, etc. there will often be critical differences in the details. When in doubt, be sure to test that your code behaves as expected, and consider checking the language specification.</p></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/devtut/generate/edit/master/docs/javascript/escape-sequences.md" target="_blank" rel="noopener noreferrer">Edit this page on GitHub</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/javascript/weakset.html" class="prev">
WeakSet
</a></span> <span class="next"><a href="/javascript/behavioral-design-patterns.html">
Behavioral Design Patterns
</a>
→
</span></p></div> </main></div><div class="global-ui"><!----></div></div>
<script src="/assets/js/app.1779e102.js" defer></script><script src="/assets/js/3.2cfa8016.js" defer></script><script src="/assets/js/1737.3e143ee1.js" defer></script>
</body>
</html>