0

I noticed here at String that contains all ascii characters they skip the first ASCII characters. Then here they list a bunch of control codes like \cA, but they only work for regular expressions. Wikipedia lists about 10 such as \n and \r. Wondering if you can write the other ones in backslash notation somehow, or you must resort to unicode \u{...} or hex \x{...}.

Lance
  • 75,200
  • 93
  • 289
  • 503

1 Answers1

2

This is covered by the spec (of course :-) ), specifically where it talks about SingleEscapeCharacter:

SingleEscapeCharacter :: one of
  ' " \ b f n r t v

Ignoring ', ", and \ which aren't control characters, that gives us:

  • \b is backspace (character 8)
  • \f is formfeed (character 12)
  • As you know, \n is newline and \r is carriage return
  • \t is tab (character 9)
  • \v is vertical tab (character 11)

So other than those six, you have to use \uNNNN, \xXX, or \u{N+} (ES2015+) for the others. Or of course use typing tricks to type them literally, which may work in some cases but is almost certainly a Very Bad Idea™. :-)


Being pedantic, these aren't ASCII, they're Unicode. But this is a place where Unicode intentionally overlaps ASCII, so...

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    You might want to mention the deprecated [`LegacyOctalEscapeSequence`](http://ecma-international.org/ecma-262/#sec-additional-syntax-string-literals) that allows to omit the `x` or `u` prefix – Bergi Aug 24 '18 at 14:22
  • 1
    @Bergi - ...or we could try to let it slip out of the general consciousness... ;-) – T.J. Crowder Aug 24 '18 at 14:30