Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make control characters programatically in Javascript?

In Javascript I can type '\u00A3' to get a character using its char code. I can do this programatically to with String.fromCharCode(parseInt('00A3', 16)).

But I can't find a way to do the same for a control character. I can type them in my source code but I want a way to generate them in code.

like image 647
fent Avatar asked Dec 05 '11 14:12

fent


People also ask

How do you create a special character in JavaScript?

JavaScript allows us to add special characters to a text String using a backslash (\) sign. We can add different types of special characters, including the single quote, double quote, ampersand, new line, tab, backspace, form feed, etc., using the backslash just before the characters.

What are control characters in JavaScript?

In computing and telecommunication, a control character or non-printing character (NPC) is a code point (a number) in a character set, that does not represent a written symbol. They are used as in-band signaling to cause effects other than the addition of a symbol to the text.

How do you display a character in JavaScript?

intro); var i = 0; // Display text, character by character var display = setInterval(function() { div. textContent += txt[i]; if (i == (txt. length-1)) { clearInterval(display); } i += 1 }, 100); } terminal('blueTh', 0);


2 Answers

Sounds to me like you could just use this list: http://en.wikipedia.org/wiki/C0_and_C1_control_codes and use the character points defined there to insert them with \u or String.fromCharCode as in your example?

PS: instead of the parseInt, you could use a literal: 0x00A3

like image 103
Gijs Avatar answered Oct 26 '22 20:10

Gijs


You can easily embed octal numbers:

var crlf = '\013' + '\012'; // octal numbers
alert('hello' + crlf + 'there'); // shows hello\n\rthere

Doesn't work the same for hex, though:

var clrf = '\0xD' + '\0xA'; // hex
alert('hello' + crlf + 'there'); // shows helloxDxAthere
like image 26
Marc B Avatar answered Oct 26 '22 18:10

Marc B