0

I have the following string:

var str = '\x27';

I have no control on it, so I cannot write it as '\\x27' for example. Whenever I print it, i get:

'

since 27 is the apostrophe. When I call .length on it, it gives me 1. This is of course correct, but how can I treat it like a not escaped string and have it print literally

\x27

and give me a length of 4?

pistacchio
  • 56,889
  • 107
  • 278
  • 420
  • Duplicate : http://stackoverflow.com/questions/6184453/how-to-make-javascript-ignore-escape-character – Vishal Kamal Dec 11 '15 at 12:22
  • 1
    No it's not. The proposed solution in that question is "You need to escape the backslash, i.e., use \\ instead of just \:" and I explicitly stated right in the second line of my question that I cannot do that, so it's a different question for a different context. – pistacchio Dec 11 '15 at 12:25
  • Why do you need to do this? - `str` is `'` and nothing else, it does not matter how it was assigned to. If you want to print 4 you would need to apply a logical test; `len = str == "'" ? 4 : str.length` - Or are you asking how to derive an escape sequence form a char? – Alex K. Dec 11 '15 at 12:28
  • @AlexK http://adventofcode.com/day/8 – pistacchio Dec 11 '15 at 12:39
  • So given `var a = "'", b = "\x27";` you want a way to determine that `b` exists in the source as an escape sequence as opposed to a literal? – Alex K. Dec 11 '15 at 12:42
  • No, given a = "\x27" I want to treat it as a four chars string starting with a '\', followed by an 'x' and '2' and '7, not like "'". – pistacchio Dec 11 '15 at 13:05
  • 1
    The only way to do this would be to parse them out of the source code itself with a regex, the escape sequence string simply does not exist at runtime – Alex K. Dec 11 '15 at 13:10

2 Answers2

1

I'm not sure if you should do what you are trying to do, but this is how it works:

var s = '\x27';
var sEncoded = '\\x' + s.charCodeAt(0).toString(16);

s is a string that contains one character, the apostrophe. The character code as a hexadecimal number is 27.

lex82
  • 11,173
  • 2
  • 44
  • 69
  • Doesn't this work only for this specific use case? What if you don't know the contents of the variable holding the string, but still need to escape characters. – Andrew Feb 25 '23 at 23:20
  • @Andrew Yes, the solution shown only solves this particular problem. But of course you can generalize it and replace all the characters in the string with their char codes in a loop. – lex82 Feb 27 '23 at 09:51
0

After the assignment var str = '\x27';, you can't tell where the contents of str came from. There's no way to find out whether a string literal was assigned, or whether the string literal contained an escape sequence. All you have is a string containing a single apostrophe character (Unicode code point U+0027). The original assignment could have been

var str = '\x27'; // or
var str = "'"; // or
var str = String.fromCodePoint(3 * 13);

There's simply no way to tell.

That said, your question looks like an XY problem. Why are you trying to print \x27 in the first place?

Community
  • 1
  • 1
nwellnhof
  • 32,319
  • 7
  • 89
  • 113