[英]How do I convert special characters from an <input> into an escape sequence
I'm writing a barcode application whereby the user has the ability to specify termination characters.我正在编写一个条形码应用程序,用户可以在其中指定终止字符。
Consider the following:考虑以下:
<input id="terminators" type="text" value="\n" />
When I read the value from the input, the escape sequence \\n
is returned as two individual characters \\
and n
.当我从输入中读取值时,转义序列\\n
作为两个单独的字符\\
和n
。 Whereas I need it as one \\n
(ASCII character 10) .而我需要它作为一个\\n
(ASCII character 10) 。
You can reproduce this issue the browsers console:您可以在浏览器控制台中重现此问题:
document.getElementById('terminators').value.charCodeAt(0) === '\n'.charCodeAt(0);
Figured it out.弄清楚了。 The value of an input is escaped with an additional \\
, so \\n
becomes \\\\n
:输入的值使用附加的\\
转义,因此\\n
变为\\\\n
:
We can validate this using the following test:我们可以使用以下测试来验证这一点:
document.getElementById('terminators').value.charCodeAt(0) === '\\n'.charCodeAt(0)
https://jsfiddle.net/v9b348z3/ https://jsfiddle.net/v9b348z3/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.