简体   繁体   English

如何将特殊字符从<input>进入转义序列

[英]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.

相关问题 如何在jQuery中转义特殊字符 - How do I escape special characters in jquery 如何在RequestContext中的Callbackparameters中转义特殊字符? - How to escape special characters from Callbackparameters in RequestContext? 如何将Javascript中的转义字符转换为.txt文件? - How do I convert escape characters in Javascript to a .txt file? 如何在我的输入中只允许数字和特殊字符? - How do I allow only numbers and special characters in my input? 如何使用javascript阻止或限制输入字段中的特殊字符 - how do i block or restrict special characters from input fields with javascript 我们如何转义输入中的一些特殊字符,类型为 12-123456 的密码看起来像 **-****** - How do we escape some special characters inside input with type password with value 12-123456 look like **-****** 在车把中,如何在字段名称中转义特殊字符? - In handlebars, how can I escape special characters in field names? 如何使用jQuery从textarea中转义特殊字符? - How to escape special characters from textarea using jQuery? 如何使用jQuery post()从文本区域转义特殊字符 - How to escape special characters from a textarea using jQuery post() 如何从JS的验证过程中区分特殊字符 - How do I discriminate special characters from a validation process in JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM