JavaScript Escape Sequences Entering special characters in strings and regular expressions

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Most printable characters can be included in string or regular expression literals just as they are, e.g.

var str = "ポケモン"; // a valid string
var regExp = /[Α-Ωα-ω]/; // matches any Greek letter without diacritics

In order to add arbitrary characters to a string or regular expression, including non-printable ones, one has to use escape sequences. 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 character code.

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 line feed (Unix newline character), with character code U+000A.

  • \n
  • \x0a
  • \u000a
  • \u{a} new in ES6, only in strings
  • \012 forbidden in string literals in strict mode and in template strings
  • \cj only in regular expressions


Got any JavaScript Question?