[英]Remove special characters from input field
I've been searching everywhere but have been unable to find exactly what I am looking for. 我一直在寻找,但一直无法找到我想要的东西。
I have an html form that is filled out with Mac addresses from our inventory so the strings inputted into the input field will look like: 我有一个html表单,其中填写了我们库存中的Mac地址,因此输入到输入字段的字符串将如下所示:
A1:A2:A3:A4:A5:A6 A1:A2:A3:A4:A5:A6
I'm trying to write a script to remove the :
character plus any spaces anywhere. 我正在尝试编写一个脚本来删除
:
字符以及任何地方的任何空格。 That way when it is entered the output will be: 这样输入输出时将是:
A1A2A3A4A5A6 A1A2A3A4A5A6
This is what I have so far: 这是我到目前为止:
<input type="text" id="macaddress" onChange="removeChar();WriteLog();" />
Then in my script I have: 然后在我的脚本中我有:
function removeChar() {
var a = document.getElementById("macaddress").value;
a = a.replace(/: /g, '');
document.getElementById.innerHTML = a;
}
I don't get any JavaScript errors with this but nothing happens. 我没有得到任何JavaScript错误但没有任何反应。
I also have another script that pulls the value of the field into a work log which is the other function WriteLog()
. 我还有另一个脚本将字段的值拉入工作日志,这是另一个函数
WriteLog()
。
Essentially I want to remove the :
then have the new value pulled into the log by the second function. 基本上我想删除
:
然后通过第二个函数将新值拉入日志。
Question spec says you want to remove :
and :
with space. 问题规范说你要删除
:
和:
空格。 Make the space in the regex optional: 使正则表达式中的空格可选:
a = a.replace(/:( )?/g, '');
But you also need to account for preceeding spaces: 但是你还需要考虑前面的空格:
a = a.replace(/( )?:( )?/g, '');
I would also trim the initial string (Just good practice) 我也会修剪初始字符串(Just good practice)
a = a.trim().replace(/( )?:( )?/g, '');
Finally, I am not sure what this line does: document.getElementById.innerHTML = a;
最后,我不确定这行是做什么的:
document.getElementById.innerHTML = a;
, but that line will throw an error. ,但该行将引发错误。 Remove it.
去掉它。
If you want to keep only numbers and letts you can use this 如果你只想保留数字和字母,你可以使用它
a.replace(/[^a-zA-Z0-9]/g, '');
which basically replaces everything that isn't az or AZ or 0-9 with an empty string. 它基本上用空字符串替换不是az或AZ或0-9的所有内容。
A great tool for explaining regex and testing it is Regex101 Regex101是解释正则表达式和测试它的一个很好的工具
And this line document.getElementById.innerHTML = a;
而这行
document.getElementById.innerHTML = a;
should be fixed as well, you probably meant something like document.getElementById('some-elements-id').innerHTML = a;
也应该修复,你可能意味着像
document.getElementById('some-elements-id').innerHTML = a;
从字符串中删除冒号和空格只需使用
str = str.replace(/[:\s]/g, '');
HTML HTML
<input type="text" id="macaddress"/>
<button onclick="removeChar()">Click me!</button>
JS JS
function removeChar() {
var a = document.getElementById("macaddress").value.trim();
a = a.split('');
a.forEach(function (character, index) {
if (character === ':') {
a.splice(index, 1);
}
});
a = a.join('');
document.getElementById("macaddress").value = a;
}
Your Regex searches for "colon immediately followed by space". 你的正则表达式搜索“冒号紧接着空格”。
If you add a pipe in between them: /:| /
如果在它们之间添加管道 :
/:| /
/:| /
, then it will search for all colons and/or spaces, in any order. /:| /
,然后它将以任何顺序搜索所有冒号和/或空格。
Demo: 演示:
function removeChar() { var a = document.getElementById("macaddress").value; a = a.replace(/:| /g, ''); document.getElementById("result").innerHTML = a; }
<input type="text" id="macaddress" onChange="removeChar();" /> <div id="result"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.