Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unescape an escaped string in JavaScript

I'm running a loop to display characters of a specific Unicode escape sequence.

var es = '';

for (var i = 0; i <= 9999; i++) {
  if (i < 10) {
    es = '\\u000' + i;
  } else if (i < 100) {
    es = '\\u00' + i;
  } else {
    es = '\\u' + i;
  }
  console.log(es);
}

...however, it doesn't display the characters, just the sequence. Is there anyway in JavaScript to unescape the escaped sequence to force it show the character?

like image 221
arnolds Avatar asked Nov 25 '25 15:11

arnolds


1 Answers

For anyone stumbling across this question (most likely my future self) looking to "unescape" a previously escaped Javascript special characters, here's a hacky way to do it:

newString = JSON.stringify(string).slice(1,-1)

However this seems to only work for certain characters, like \t, \n, \r, \f, \b, \\, etc.

To explain myself better, in the case of tabs, this will convert a tab character back into literal/raw string that is \t.

like image 108
V. Rubinetti Avatar answered Nov 28 '25 05:11

V. Rubinetti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!