1

A simple one here.

I have an HTML <textarea> in which I want to allow the user to enter escape sequence. I.e.:

Hello\nworld\x21

And I want my script to "interpret" the \n and the \x21 to get:

Hello
world!

Thanks

EDIT: The purpose of this is to allow the user to enter thermal printer's code in an html page. The printer needs special chars. I want to take that string, "interpret" the special codes and send it to the printer. Currently, the printer prints the string as-is.

RamenChef
  • 5,557
  • 11
  • 31
  • 43
vIceBerg
  • 4,197
  • 5
  • 40
  • 53
  • 1
    [what have you tried](http://whathaveyoutried.com) and what issues did you have in your attempt? – zzzzBov May 31 '16 at 13:56
  • 1
    Looks alot like this: http://stackoverflow.com/questions/1787322/htmlspecialchars-equivalent-in-javascript/4835406#4835406 You need to make a converter and split the string i guess. – Bram B May 31 '16 at 13:56
  • also, what is your expected output for backslashes used for non-escapes? for example what should ``\`` produce, or ``\ ``? – zzzzBov May 31 '16 at 14:00
  • I added an edit to explain my needs – vIceBerg May 31 '16 at 14:05
  • @zzzzBov I do not need output. I send the string to a thermal printer – vIceBerg May 31 '16 at 14:06
  • 1
    @vIceBerg, I don't really care what you *do* with the output. I'm asking what your expectations are, because it seems as though you haven't really defined the behavior yet. For some input string `in` you expect some output string of `out`, to properly answer the question requires that the input and output expectations are well defined. As it stands your question reads more like a work order. You haven't shown any attempt at solving it yourself, so I'm going to vote to close this as too broad, although "unclear" would also be appropriate. – zzzzBov May 31 '16 at 14:10
  • @zzzzBov I just don't understand how can I be more precise. My user enters a string in which it has escaped sequence in it and I want those escape sequence to be change by it's real char representation. So it I have a string with litteral \x21 in it, I want it to be converted to ! – vIceBerg May 31 '16 at 14:14
  • @vIceBerg, I already asked what ``\`` and ``\ `` would produce. What about `\u0021`? How about `\u{0000000000000021}`? Are you expecting standard JavaScript string escapes to work? Would `\0` produce a null character and break anything? My point is that a naive question begets a naive answer. – zzzzBov May 31 '16 at 14:34

1 Answers1

0

The only way I can think of that doesn't use a long replace line is with the eval() function. You could, for example, use the following:

function useEscapes(s) {
    return eval('("' + s.replace(/([^\\]|^)(?=(\\\\)*["\n])/g, '$1\\').replace(/\n/g, 'n') + '")');
}

The replace is to make sure it's safe to pass into the eval() function.

EDIT 9/28/2016:

There's a better way that isn't a security risk. Because a string literal is valid JSON, you can wrap it in quotes and use JSON.parse. You can also possibly escape quotes in it so that users don't get inexplicable syntax errors. If an attacker tries to put code outside the string, it will just throw a syntax error.

function useEscapes(s) {
    return JSON.parse('"' + s
        .replace(/((^|[^\\])(\\\\)*)"/g, '$1\\\\"') // optional - prevents syntax errors
        + '"');
}
RamenChef
  • 5,557
  • 11
  • 31
  • 43
  • 1
    The only "bug" is if there's a carriage return in the user's input string, the function returns a Unexpected token ILLEGAL error. – vIceBerg May 31 '16 at 14:30
  • 2
    Good luck with `'"'` as input, and moreso for having opened your app to a user being able to unknowingly execute arbitrary JavaScript. – zzzzBov May 31 '16 at 14:36
  • 1
    Say it's an oversight. Second replace optional. – RamenChef May 31 '16 at 14:42