[英]How to strip HTML ASCII codes from a string using Javascript
I have a string that I'm building dynamically while a user is entering data into a textbox, I'm capturing input as it's being entered and saving it to a global variable as follows: 当用户在文本框中输入数据时,我有一个动态构建的字符串,正在捕获输入时捕获的输入,并将其保存到全局变量中,如下所示:
eg 例如
var uid = '';
function buildString(e) {
var keynum = e.keyCode ? e.keyCode : e.which;
uid += String.fromCharCode(keynum);
}
I notice that the string has HTML ASCII codes appended in front of each character that was typed. 我注意到该字符串在每个键入的字符前面都附加了HTML ASCII代码。
eg 例如
091[041)062>030RS04800546029GS
091 [041)062> 030RS04800546029GS
so 所以
091 = [ 091 = [
041 = ) 041 =)
062 = > 062 =>
048 = 0 048 = 0
etc. 等等
I don't want these codes to be present in the string, is there a regex, or some other method in Javascript, that will strip just the codes out without stripping actual, valid, numbers that were entered? 我不希望这些代码出现在字符串中,是否有正则表达式或Javascript中的其他方法会在不删除输入的实际有效数字的情况下仅删除代码?
It sounds like you're not using the best event for what you're trying to do. 听起来您没有将最好的事件用于尝试做的事情。 Your code should mostly work for the
keypress
event ( example ), but won't work well at all for keyup
or keydown
. 您的代码大部分应该适用于
keypress
事件( 示例 ),但对于keyup
或keydown
根本无法正常工作。
But just listening for keypress
won't let you build up a string of what keys were pressed. 但是,仅听
keypress
不会让您积累一系列按键的信息。 For instance, using the example above, if I type "abcd" then right-arrow twice then "xx", the built-up string will be "abcdxx" where what's showing in the text box is "abxxcd". 例如,使用上面的示例,如果我键入“ abcd”,然后右击两次,然后键入“ xx”,则内置字符串将为“ abcdxx”,其中文本框中显示的内容为“ abxxcd”。 So more sophistication will be required.
因此,将需要更多的复杂性。
Here there be dragons. 这里有龙。 For a good idea of the madness around JavaScript keyboard events, this site has great information.
要很好地了解有关JavaScript键盘事件的疯狂之处, 此站点提供了大量信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.